我有一个类别列表:
cat1
cat2
cat3
cat4
.
.
.
cat8
我用这个代码显示它:
<?php
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($con,"adminsys") or die ("no database");
$query = "SELECT * FROM productmodelcategory";
$results = mysqli_query($con, $query)or die("Connection could not be established");
echo "<select class='categoryoption' name='categorylist' id='categorylist' onChange='dropdownlistchange(this);'>";
while ($row1 = mysqli_fetch_assoc($results))
{
$dis1 = "<option value='".$row1['Category']."'>".$row1['Category']."</option>";
echo $dis1;
}
echo "</select>";
?>
我正在尝试使用这个列表来显示mysql中数据库中的数据。我知道我想做的是,当我想显示mysql中的数据时,我使用上面的代码在下拉菜单中使用列表进行显示,但如果我存储的数据不是"cat1",我不知道如何显示列表,例如,它可能是cat4或cat6。我该如何根据我在mysql中存储的内容来显示特定的行?
您可以尝试以下代码:
while ($row1=mysqli_fetch_assoc($results)){
if($row1['Category'] == $storedData){ //$storedData is what you stored in MYSQL
$selected = "selected";
}else{
$selected = "";
}
$dis1 = "<option value='".$row1['Category']." ".$selected."'>".$row1['Category']."</option>";
echo $dis1;
}