Forums

PHP

This topic is locked

Sorted Lists like phpMyAdmin Tables.

Posted 20 Jan 2006 23:55:13
1
has voted
20 Jan 2006 23:55:13 Carlos Bravo posted:
Hi people....

I work with phpMyAdmin..
and i notice that when i have a : table>Examinate (the listed items of a table)

we can sort the list clicking each header ...

My question is: What code is that to sort a list like that??

I need this efect in my list ...
please i need a not complicated answer .... (level basic)

Thanks in advanced!!!

Carlos Bravo <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>
Lima - PERU


Replies

Replied 25 Jan 2006 18:16:26
25 Jan 2006 18:16:26 Roddy Dairion replied:
There is no easy way of doing it. This is a easy as i can get.
1st Step: extract your data from your db into a table on your webpage.
2nd Step: Make your table headers links (&lt;a href="yourpage.com?sort='&lt;?=$sort?&gt;'"&gt;Your Text&lt;/a&gt; make sure it redirects on the same page
3rd Step: We start coding in php.
&lt;?php
$sort="1";
if ($_GET['sort']=="1"
{
$sort="2";
$order="order by --the header in the table you want to sort-- DESC";
}
else if ($_GET['sort'] == "2"
{
$sort="1";
$order="order by --the header in the table you want to sort-- ASC";
}
?&gt;
I explained the code above. When you page is loaded in your link the parameter sort will equal to 1. If you click on this when it equals to 1 if reloads the same page and sets sort to 2. And ready in orer accordingly. Now the next step it to set your variable $order in the query.

now your mysql query
&lt;?php
$query="select * from table1 where field1=value $order";
?&gt;
Here according to the sorrt number it will change the value in order.

You can do it for each field. Try to figure it out. If you can't then come back to me.

Hope you understand.
Regards,
Roddy

Reply to this topic