我从表中选择一个id,然后通过id获取所有成员的ID。现在的问题是,如何按字母顺序对名称($ uname)进行排序?它可能很简单,但我卡住了...谢谢你的帮助! 以下代码 -
$sql= mysql_query("Select DISTINCT(`Supervisor ID`) from `list` ");
while($row = mysql_fetch_array($sql))
{
$id = $row['Supervisor ID'];
$query = "SELECT `Name` from `list` WHERE `ID` = '$id' ";
$name = mysql_query($query);
$username = mysql_fetch_array($name);
$uname = $username['0'];
$Supervisor .="<OPTION VALUE=\"$uname\">".$uname.'</option>';
}
答案 0 :(得分:3)
ORDER BY
子句用于从数据库进行的任何类型的排序。
因此,在循环中更改您的查询,如下所示:
$query = "SELECT `Name` from `list` WHERE `ID` = '$id' ORDER BY Name ASC ";
有关详细信息:ORDER BY cluase
让我知道以获得进一步的帮助。
答案 1 :(得分:1)
你可以在单个查询中完成,试试这个..
$sql= mysql_query("Select DISTINCT Supervisor ID, Name from list ORDER BY Name ASC");
while($row = mysql_fetch_array($sql))
{
$uname = $row['1'];
$Supervisor .="<OPTION VALUE=\"$uname\">".$uname.'</option>';
}
答案 2 :(得分:0)
使用此: -
$sql= mysql_query("Select DISTINCT(`Supervisor ID`) from `list` ");
while($row = mysql_fetch_array($sql))
{
$id = $row['Supervisor ID'];
$query = "SELECT `Name` from `list` WHERE `ID` = '$id' ";
$name = mysql_query($query);
$username = mysql_fetch_array($name);
/* Sore name in to an new array */
$uname[] = $username['0'];
}
使用
对您的名称数组进行排序sort()
Click Here For Demo of Sort function
$user_names = sort($uname);
像这样创建选项valus
foreach($uname as $name) {
$Supervisor .="<OPTION VALUE=\"$name\">".$name.'</option>';
}
答案 3 :(得分:0)
SELECT `Name` from `list` WHERE `ID` = '$id' order by `Name` ASC
使用order by
子句进行升序,使用desc进行降序