У меня есть страница updateProducts, где я хочу показать категорию вниз и в раскрывающемся category pertaining to a particular product must be shown on the top of the list
. Я хочу, чтобы эта category pertaining to a particular product must be shown on the top of the list
. Это означает, что в раскрывающемся списке должны отображаться все категории, но выбранная категория должна отображаться в верхней части списка.
Code:
$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>";