Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question shorting the dropdown

    Ok i have a question. Is there any way to short down the drop down in "Alphabetical" order.

    I have a drop down select list which gets its values from mysql DB and i want it to sort it down in alphabetical order to easy selection for the user.

    Thanks in advance.

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    if youre getting the values from a mysql db presumably you are creating the select list through php, just get the php to order the list when it creates it. So in your select statement use 'select * from db order by name' or whatever your field or db is.
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #3
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    Quote Originally Posted by Prashank View Post
    Ok i have a question. Is there any way to short down the drop down in "Alphabetical" order.

    I have a drop down select list which gets its values from mysql DB and i want it to sort it down in alphabetical order to easy selection for the user.

    Thanks in advance.

    If you have to do this with javascript, you can try something like this:

    HTML:
    Code:
    <select id="s">
        <option value="f">f</option>
        <option value="d">d</option>
        <option value="a">a</option>
        <option value="c">c</option>
        <option value="b">b</option>
        <option value="g">g</option>
        <option value="e">e</option>
    </select>

    Javascript:
    Code:
    function ASC(v1, v2) {
        if (v1 < v2) {
            return -1;
        } else if (v1 > v2) {
           return 1;
        } else {
           return 0;
        }
    }
        
    var select = document.getElementById('s')
          opts = select.getElementsByTagName('option'),
           len = opts.length,
             i = 0, 
             j = 0,
       arrVals = [];
    
    for (i; i < len; i += 1) {
       arrVals[i] = opts[i].value; 
    }
    
    var ordered = arrVals.sort(ASC);
    
    for (j; j < len; j += 1) {
       opts[j].value = ordered[j];
       opts[j].text =  ordered[j];
    }




    I don't know, I don't care, and it doesn't make any difference!
    -Albert Einstein-




  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    Thanks friends!
    I used mysql sorting and got the solution...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •