我有一个 updateProducts 页面,我想在其中显示类别下拉列表,并且在下拉列表中我希望与特定产品相关的类别必须显示在列表顶部
。这意味着,下拉列表必须显示所有类别,但所选类别应显示在列表顶部。
代码:
$pid=$_GET['id']; //getting the id from another page
$data=mysqli_query($con,"SELECT * FROM products WHERE pid='$pid'");
while($row=mysqli_fetch_array($data))
{
echo "<td><img src='../product_images/".$row['image']."' height='66px' width='66px' ></td>";
echo "<td><input type='text' value='".$row['title']."'></td>";
echo "<td><input type='text' value='".$row['body']."'></td>";
echo "<td><input type='text' value='".$row['cost']."'></td>";
}
$data2=mysqli_query($con,"SELECT * FROM category");
echo "<td><select>";
echo "<option>Category</option>";
while($row2=mysqli_fetch_array($data2))
{
echo "<option>".$row2['category']."</option>";
}
echo "</select></td>";
最佳答案
首先循环结果并分离最上面的结果,然后输出选择。
$cid=??;
$display_top;
$display_rest=array();
while($row2=mysqli_fetch_array($data2))
{
//if id == on_top_id set the variable on_top
if($row2['id'] === $cid){
$display_top= "<option>".$row2['category']."</option>";
}
//the rest
else{
$display_rest[] = "<option>".$row2['category']."</option>";
}
}
//output the select
echo "<td><select>";
echo "<option>Category</option>";
//first echo the id_on_top
echo $display_top;
//echo the rest
echo implode('',$display_rest);
echo "</select></td>";
关于php - 在下拉列表顶部显示所选类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49064216/