order by drop down list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    #1

    order by drop down list

    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
    Recognized Expert New Member
    • Oct 2008
    • 228

    #2
    Hi,

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

    Code:
    function orderby() {
    
      var selObj = document.getElementById("cbo_OrderBy");
      var order_by = selObj.options[selObj.selectedIndex].value;
    
      location.href = "somepage.asp&orderby=" + order_by;
    
    }
    
    
    <select id = "cbo_OrderBy" onChange="orderby();">
    <option value="date">order by date</option>
    <option value="name">order by name</option>
    <option value="category">order by category</option>
    </select>
    Obviously "somepage.a sp" 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.

    Comment

    • fran7
      New Member
      • Jul 2006
      • 229

      #3
      Thanks Gaz, just what i wanted and got it working fine.
      Thanks
      Richard

      Comment

      • GazMathias
        Recognized Expert New Member
        • Oct 2008
        • 228

        #4
        No problem,

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

        Code:
        somepage.asp&orderby=" + order_by;
        Should be:

        Code:
        somepage.asp?orderby=" + order_by;
        It was before me first coffee of the day!

        Comment

        Working...