PDA

View Full Version : sorting


CenZ
03-06-2006, 08:34 PM
Hi, i got a MySQL database that i dynamicly input information to through forms. what im wondering how to do is this, its broken into different feilds,
Item, Description, Price, Website, Donor what i want to be able to do is click the feild and it sorts it alphabeticly or for price most expensive to cheapest. any help would be great. thanks alot. you will see me around more :)

Creative Insanity
03-06-2006, 08:58 PM
In DW when you make your recordset at the bottom of the window is a sort option, just click in that and select the field you want to sort by and then in the option next to that select either dec or acc.

CenZ
03-06-2006, 09:53 PM
how would i do it on click, like when they view the page its ID ASC and then when they click Name it sorts Name ASC

Creative Insanity
03-06-2006, 11:00 PM
For the product or name page, when creating the recordset for that page make the sort method name instead of ID.

CenZ
03-06-2006, 11:33 PM
well what if i want them all on one page then when you click type it sorts all of them by type and the same for the rest instead of creating a page for all of them

CenZ
03-07-2006, 08:07 PM
bump, any idea?

CenZ
03-07-2006, 10:32 PM
incase you cant understand what im trying to do since im not very good at explaining it. something like this www.domain.com/page.php?sort=type then it will sort by type, then have page.php?sort=item and it will sort by the item.

davidj
03-08-2006, 09:00 AM
this is easy if you know a small bit of php

use your column headers of your dynamic table

example...

header1 | header2 | header3 | header4

now turn thes into links

header1 (samepage.php)

now just add....

header1 (samepage.php?sort=1)
header2 (samepage.php?sort=2)
header3 (samepage.php?sort=3)
now in your php catch the var sort

$sort = $_GET['sort'];

then do a switch...

switch($sort){
case '1': $sqlsort = "order by field1 desc"; break;
case '2': $sqlsort = "order by field2 desc"; break;
case '3': $sqlsort = "order by field3 desc"; break;

}

now you should have a recordset that is getting the data that shows you the table (with your header links)

mysql_select_db($database_pfm, $pfm);
$query_rs = sprintf("SELECT * FROM pfm_jobs where pfm_jobs.pfm_jobs_id ='%s' %s", $var, $sqlsort); //****** using $sqlsort here to set order from switch
$rs = mysql_query($query_rs, $pfm) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

CenZ
03-08-2006, 04:55 PM
i followed you up to mysql_select_db($database_pfm, $pfm);
$query_rs = sprintf("SELECT * FROM pfm_jobs where pfm_jobs.pfm_jobs_id ='%s' %s", $var, $sqlsort); //****** using $sqlsort here to set order from switch
$rs = mysql_query($query_rs, $pfm) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
lol i dont know what alot of that is, my SQL querys dont look like that lol

davidj
03-08-2006, 05:05 PM
post the recordset code you have in your page

the one which populates your table..

to put it simpler all you are doing is where you would have your SELECT statement and you would add a ORDER BY FIELD just remove that and replace it with a %s, then use the $sqlsort variable to populate it

make sure your SELECT is in an sprintf function and your flying.