Page 1 of 1

Building a query string

Posted: Wed Nov 10, 2004 4:58 am
by hairyjim
Hi all,

I have a page that lists many records from a db.

Now I wish the user to be able to sort the list by the titles on the columns which I have done by passing an $orderby via <a href> as below:

Code: Select all

echo "<td height="15"><a href="".$_SERVER&#1111;'PHP_SELF']."?orderby=title">Title</a></td>";
echo "<td><a href="".$_SERVER&#1111;'PHP_SELF']."?orderby=creation_date">Creation date</a></td>";
echo "<td><a href="".$_SERVER&#1111;'PHP_SELF']."?orderby=counter">Views</a></td>";
echo "<td><a href="".$_SERVER&#1111;'PHP_SELF']."?orderby=product">Product</a></td>";
Further to this I only show 15 records per page so a pageid is also passed via the url.

Code: Select all

//this is not all the code just a snippet to show how I build the url string.
for ($i = 0; $i < $pages; $i++) &#123; 
$url = "index.php?pageid=" . $i;
Now both of these work just fine independently from one another. But now I have come to a bit of a head scratcher.

How do I build the url to either one of the above so that it takes into account any other actions the user may have performed on the page such as sort by title then clicks to view page 2, or is viewing page 2 then clicks to sort by date.

I hope I make sense.

I looked through the manual and I found $_SERVER["QUERY_STRING"]; which brings back any query after the foo.php but I am not sure how I should or can utilise this to help.

Could someone enlighten me please.

Posted: Wed Nov 10, 2004 6:07 am
by CoderGoblin
You need to read a manual or tutorial on the $_GET, $_POST and $_REQUEST variables. These are an indexed array of values passed such as...

$_GET['orderby']="creation_date" or
$_GET['page_id']=x

you can use the isset($_GET['orderby']); to see if a value exists.

While you are looking up things I would also recommend you look up the PHP commands "urlencode" and "urldecode".

$_REQUEST contains both $_POST and $_GET variables.

A useful place to start is by looking at form handling as the process is the same.