请告诉我如何以html / php格式按字母顺序对Dropdown值的视图进行排序。
以下是代码:
<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>
答案 0 :(得分:4)
您可以通过SQL订购,这总是比使用PHP排序更快:
select * from bank_details order by bank_name ASC
这是A-Z排序
select * from bank_details order by bank_name DESC
这是Z-A
如果您愿意,也可以按多个列进行排序,只需在逗号分隔列表中添加每个列,就像选择列列表一样:
select * from bank_details order by bank_name, city_name DESC
请注意,这将按城市名称降序(Z-A)和bank_name按默认(A-Z或升序)排序。所以,你可以这样做:
select * from bank_details order by bank_name DESC, city_name DESC
此外,由于有默认值,您可以这样做:
select * from bank_details order by bank_name