Sorting Results
Moderator: General Moderators
-
billybonz81
- Forum Newbie
- Posts: 3
- Joined: Sat Oct 02, 2004 2:43 am
- Location: baytown, tx
Sorting Results
does anyone know of a way of using hyperlinks to sort the results of a query?
Not 100% sure what you mean, but I think you'd need to pass the sorting field(s) in the URL.
So, for example, if you want to sort by the field 'age', you'd have a link that looks like:
Then in the page, just have your query ordered by $_GET['sort']
So, for example, if you want to sort by the field 'age', you'd have a link that looks like:
Code: Select all
<a href = 'this_page.php?sort=age'>Age</a>Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Have the link include how you want to sort it and then use this information to produce the sql query.
You can make this as complex or simple as you like allowing them to be sorted in different directions and fields.
Code: Select all
$query = "SELECT * FROM blah";
if(isset($_GET['sort'])){
if($_GET['sort']=='title'){
$query .= " ORDER BY title";
}else{
$query .= " ORDER BY id";
}
}-
billybonz81
- Forum Newbie
- Posts: 3
- Joined: Sat Oct 02, 2004 2:43 am
- Location: baytown, tx
still having problems!
nigma | Help us, help you. Please use
nigma | Help us, help you. Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
ok i did it and now its giving me the following error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc LIMIT 0, 10' at line 1
here is my code:Code: Select all
$query_Recordset1 = "SELECT * FROM `data`";
if (isset($_GET['sort'])) {
if($_GET['sort'] =='id') {
$query_Recordset1 .= " ORDER BY id asc";
}else {
$query_Recordset1 .= " ORDER BY beds asc";
}
}
if (isset($_GET['sort2'])) {
if($_GET['sort2'] =='id') {
$query_Recordset1 .= " ORDER BY id asc desc";
}else {
$query_Recordset1 .= " ORDER BY beds desc";
}
}
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]