order by/sort/refresh... where to start

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

order by/sort/refresh... where to start

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

second link from the Useful Posts thread: viewtopic.php?t=25082
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post 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/ :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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))
{
   //
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Post Reply