Connecting Tech Pros Worldwide Forums | Help | Site Map

order by drop down list

Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#1: Jun 4 '09
Hi I wonder if anyone could point me in the right direction. I order the data on my page by the date added asc. I would like the user to be able to order the results from a drop down list like

order by date
order by name asc
order by category

etc.....
Any ideas how this is done would be great.
Thanks
Richard

GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#2: Jun 5 '09

re: order by drop down list


Hi,

I would use JavaScript to send the value of the drop down onChange.

Expand|Select|Wrap|Line Numbers
  1. function orderby() {
  2.  
  3.   var selObj = document.getElementById("cbo_OrderBy");
  4.   var order_by = selObj.options[selObj.selectedIndex].value;
  5.  
  6.   location.href = "somepage.asp&orderby=" + order_by;
  7.  
  8. }
  9.  
  10.  
  11. <select id = "cbo_OrderBy" onChange="orderby();">
  12. <option value="date">order by date</option>
  13. <option value="name">order by name</option>
  14. <option value="category">order by category</option>
  15. </select>
  16.  
Obviously "somepage.asp" would be the same page.

It would then just be a matter of modifying the SQL statement to use the value passed in the Query String.

Gaz.
Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#3: Jun 5 '09

re: order by drop down list


Thanks Gaz, just what i wanted and got it working fine.
Thanks
Richard
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#4: Jun 5 '09

re: order by drop down list


No problem,

For anyone else using this code in the future, notice the typo in the JavaScript :

Expand|Select|Wrap|Line Numbers
  1. somepage.asp&orderby=" + order_by;
  2.  
Should be:

Expand|Select|Wrap|Line Numbers
  1. somepage.asp?orderby=" + order_by;
  2.  
It was before me first coffee of the day!
Reply