PHP and links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

PHP and links

Post by lloydsmods »

We've all seen those sites that display data in table form and which give you HTML style links to re-sort the data, maybe in alphabetical order, by date, etc. Can this be accomplished in PHP without creating a new page for each SELECT query? Click on a link and it passes a value to a function which initiates a query and displays the sorted data. Can that sort of thing be done on one (or two pages) using PHP?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes, you can write code in PHP to do that.

You have the page link to itself passing a ?sort_by=alpha or ?sort_by=date, etc at the end of the URL. Use _GET["sort_by"] at the top of the script to retrieve the selected value. If no sort_by is provided, set it to some default.

Then write a switch/ if-elseif-else tree to create the appropriate query.

You can use the same code to display the results of any of the queries.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

since you asked a simple question (for most people anyway), here is what you gota do:

have query like:

Code: Select all

if(!= $sort)
{
$sort = "field_name"; // if it is not set, it will show this
}
$query = mysql_query("select field1, field2 from table order by $sort");

//then the rest of db querys/printing
then you need a link to pass $sort in the query
<a href='pagename.php?sort=fieldname'>Sort by field</a>

hope that helps you out....
Last edited by qads on Wed Aug 21, 2002 3:37 pm, edited 1 time in total.
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Great...one more

Post by lloydsmods »

Great. Thanks for the help.

Probably this is a simple guestion as well, but I'm a relative newbie so indulge me:

Lets say you query a table and print a list of customer names. You want details of customer transactions or other data by clicking on the names produced by the original query. Can someone suggest a way to do this? I'm probably making this much harder than it should be so if someone please point me in the right direction.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

I'd suggest that you pull the id of the customer from the table, then attach it to the end of the url of the link. Then, retrieve the GET variable (let's imagine it's $id = $_GET['id']; ) from the page you send it to. Use this to pull info about the customer with a query like:
$query = "
SELECT * FROM customer, transactions
WHERE customer.customer_id = {$id}
AND transaction.customer_id = {$id}";
Post Reply