...

View Full Version : shorting the dropdown



Prashank
01-08-2012, 12:18 PM
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.

nomanic
01-08-2012, 12:59 PM
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.

oVTech
01-08-2012, 01:16 PM
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:



<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:



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];
}

Prashank
01-08-2012, 02:30 PM
Thanks friends!
I used mysql sorting and got the solution...:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum