简体   繁体   中英

Alphabetically Sort Dropdown values in php/html forms?

Please Tell me how to Alphabetically Sort view of Dropdown Values in html/php forms.

Here is the Code :

<select name="bank" id="bank" style='text-transform:uppercase'  required/>
                                                 <option></option>
                                                 <?php $bank_name=mysql_query("select * from bank_details")or die(mysql_error()); 
                                                 while ($row=mysql_fetch_array($bank_name)){                                                
                                                 ?>
                                                 <?php
                                                $bankname = $row['bank_name'];
                                                $bankname = mb_strtoupper($bankname);
                                                ?>
                                                <?php
                                                $bankifsc = $row['bank_ifsc'];
                                                $bankifsc = mb_strtoupper($bankifsc);
                                                ?>

                                                 <option style='sort'> <?php echo $bankname ?> - <?php echo $bankifsc ?> </option>
                                                 <?php } ?>
                                               </select>

You can order by SQL, which will always be faster than sorting with PHP:

select * from bank_details order by bank_name ASC

This is AZ ordering

select * from bank_details order by bank_name DESC

This is ZA

You can also order by multiple columns if you'd like, just add each in a comma separated list just like your select column list:

select * from bank_details order by bank_name, city_name DESC

Note that this will order by city name descending (ZA) and bank_name by the default (AZ, or ascending). So, you can do:

select * from bank_details order by bank_name DESC, city_name DESC

Also, since there's a default, you can just do:

select * from bank_details order by bank_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM