sorting acs and desc
i have select out a list of record, 'name, and 'date'. how can i do it in such a way that when i clicked the title 'name', all the records below will sort by name, asc and when i clicked another time(name), the records below will arrange by desc order. that goes the same with 'date'.
how can i do that??
Wil posted this at 09:37 — 16th January 2002.
They have: 601 posts
Joined: Nov 2001
You're using a SELECT statment on a SQL database, right? It's quite hard to help you without more information, but usually you just need to tell your SQL server in what order you want the results returned.
You need something like this:
SELECT field FROM database ORDER BY field
- wil
joyce posted this at 09:43 — 16th January 2002.
They have: 164 posts
Joined: Nov 2001
i have this three field:
<?php
print \"<tr><td width=30% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Date</font></td>\n\";
print \"<td width=40% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Name</font></td>\n\";
print \"<td width=30% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Expected Salary</font></td></tr>\n\";
?>
they are actually titles, below them will have a list of records. how am i going to do it in such a way that when i clicked the title 'name, all the records i have below will sort by acsending and an arrow pointing up and when i clicked one more time, they will sort by descending order and an arrow pointing down.
Wil posted this at 09:51 — 16th January 2002.
They have: 601 posts
Joined: Nov 2001
By using ORDER BY and passing the field you want ordered by to the script. Your script will then query the database using the ORDER BY field passed to it.
To get the correct up/down images, you will need to establish what value the sort order is, and then do a conditional statement to work out what image to display.
Have you done any coding yet? If so, could you post the relevant bits? As in, how are you retrieving the records from the database?
- wil
joyce posted this at 09:58 — 16th January 2002.
They have: 164 posts
Joined: Nov 2001
this is the $query i used to select out the records..
<?php
$sql = \"select date, uname, exsal from biopersonal where publish='y' and (job='database' or job1='database' or job2='database')\";
$result=mysql_query($sql);
?>
Wil posted this at 11:05 — 16th January 2002.
They have: 601 posts
Joined: Nov 2001
OK. Now, using the information I provided in the above post you can modify this query to be sorted by a paticular field.
Pass the order you want the results to the script. If no order is passed, I'll assume that you want them sorted by "uname".
$sort = $query->param('sort') || "uname";
$sql = "select date, uname, exsal from biopersonal where publish='y'
and (job='database' or job1='database' or job2='database') <strong>ORDER BY $sort</strong>";
$result = mysql_query($sql);
[ untested code ]
- wil
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.