我在PHP中有一个表单。它没有被排除。我想对它进行排序。我怎么会这样做呢。这是我的以下PHP代码。提前感谢您的帮助。
<select id="school_name" name="school_name">
<option value="">Select School</option>
<?php for($c=0; $c<sizeof($school_details); $c++){?>
<option value="<?php echo stripslashes($school_details[$c]["title"]); ?>"
<?php if($school_details[$c]["title"]==$appInfo["school_name"]){?> selected="selected" <?php } ?> ><?php echo stripslashes($school_details[$c]["title"]); ?>
</option>
<?php } ?>
</select>
答案 0 :(得分:2)
尝试这样的事情:
$str = "<select id='school_name' name='school_name'>";
$options = "";
for($i = 0; $i<10;$i++)
{
$RandomNum = rand(0, 100);
$options = "" . $options . ("<option value='$RandomNum'> " . $RandomNum . "  </option>\n");
}
$optionsArray = explode("\n", $options);
natsort($optionsArray);
$sorted_option = implode("|",$optionsArray);
$select = $str.$sorted_option."</select>";
echo $select;
答案 1 :(得分:0)
PHP提供了许多排序方法:http://www.php.net/manual/en/array.sorting.php
答案 2 :(得分:0)
您可以实现用户定义的排序来实现此目的。假设你想按照标题的价值排序&#39;您只需在打开选择标记之前添加此元素:
<?php
uasort($school_details,'cmp');
function cmp($a,$b){
if($a['title'] == $b['title']){return 0;}
return ($a['title'] < $b['title']) ? -1 : 1;
}
?>