Page 1 of 1
PHP and links
Posted: Wed Aug 21, 2002 1:27 pm
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?
Posted: Wed Aug 21, 2002 1:33 pm
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.
Posted: Wed Aug 21, 2002 3:30 pm
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....
Great...one more
Posted: Wed Aug 28, 2002 2:41 pm
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.
Posted: Wed Aug 28, 2002 3:35 pm
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}";