Page 1 of 1
order by/sort/refresh... where to start
Posted: Thu Aug 25, 2005 4:52 pm
by imstupid
hello again-
I've got a company directory where a basic,
Code: Select all
while($row = mysql_fetch_array( $result )
prints out the entire contents into a table. I was hoping to create some sort of arrow icons at the top of the fields that when clicked, would reprint-out the entire table in a different order. For instance, initially, the directory is displayed by last name descending, but if a user wants to start with the last names first, they hit the arrow and the table is refreshed and redisplayed in ascending order. I guess since it is only 3 fields, with ASC and DESC for each field, I could just have the arrows as links to 6 different php files, but I was wondering if there was a way to do it in only one file. If so, can some one give me advice on where to start looking, or what to search for? I've been reading the manual on sort psort asort... you name it, but am kind of lost. Any clues as to where to begin so I can attempt to figure this out for myself would be appreciated.
Posted: Thu Aug 25, 2005 4:56 pm
by feyd
second link from the Useful Posts thread:
viewtopic.php?t=25082
Posted: Thu Aug 25, 2005 6:11 pm
by Stewsburntmonkey
This can actually be done easily with Javascript as well.
Here is a very nice javascript solution that only requires you to declare the table you want to make sortable as class="sorttable".
http://www.kryogenix.org/code/browser/sorttable/ 
Posted: Thu Aug 25, 2005 10:33 pm
by s.dot
Tack a variable onto the URL and perform your query based on that
Example:
Code: Select all
// Example URLs
// http://www.domain.com/list.php?sort=lastname
// http://www.domain.com/list.php?sort=firstname
// http://www.domain.com/list.php?sort=othersort
switch($_GET['sort'])
{
case "lastname":
$result = mysql_query("SELECT * FROM table ORDER BY lastname ASC");
break;
case "firstname":
$result = mysql_query("SELECT * FROM table ORDER BY firstname ASC");
break;
case "othersort":
$result = mysql_query("SELECT * FROM table ORDER BY othersort ASC");
break;
}
// loop through
while($array = mysql_fetch_array($result))
{
//
}
Posted: Thu Aug 25, 2005 11:18 pm
by imstupid
thanks guys- I'll check out these tomorrow and get back to you. just found out my condo association owes 1.1 million bucks to the city, making me responsible for paying 12 grand (USD) in 60 days. I've got to drink a few beers and go to sleep.
j
Posted: Thu Aug 25, 2005 11:39 pm
by feyd
imstupid wrote:just found out my condo association owes 1.1 million bucks to the city, making me responsible for paying 12 grand (USD) in 60 days.
8O8O8O8O8O8O
Posted: Fri Aug 26, 2005 12:51 pm
by jayshields
another idea would be make images for your arrows and make those images into form submit buttons (named asc, des, w/e), then the method as post for the form, then when a user clicks an arrow, send the form back to the same page and use something like this
Code: Select all
if (isset($_POST['ascarrow'])) {
//query the db with ORDER BY ASC and print the whole table
}
if (isset($_POST['desarow'])) {
//query the db with ORDER BY DES and print the whole table
}
i would personally do something like that but im sure there are many other ways around it.
gl.