我正在对产品的价值进行排名,并将其列在下拉菜单中。我似乎无法从下拉菜单中保留所选的排名值。任何帮助将不胜感激,提前谢谢。
<?php
$sessionid = $_SESSION['uid'];
$period = ". ";
$info = $conn->prepare("SELECT `productid`,`name` FROM `products` WHERE id = :id ORDER BY `name` DESC ");
$info ->bindParam(':id', $sessionid , PDO::PARAM_INT);
$info->execute();
$rank = 0;
$last_score = false;
$rows = 0;
$ops = '';
while ($userinfo = $info->fetchobject()) {
$rows++;
if( $last_score!= $userinfo->name ){
$last_score = $userinfo->name;
$rank++;
}
$productid1 = "$userinfo->productid";
$name1 = "$userinfo->name";
$ops.= "<option value='" . $productid1 . "'>" . $rank . "" . $period . "" . $name1 . "</option>";
}
?>
<form action="store.php" method="POST">
<b>Select a product from our top ranking product list. </b> </br>
<select name= "products" >
<?php echo $ops ?> <?php if($_POST['products']=='$ops') echo "selected = \"selected\""; ?>>
</select>
</br>
<input type="submit" name="Submit" value="Submit"/>
</form>
答案 0 :(得分:0)
这在你拥有它的地方不起作用:
if($_POST['products']=='$ops') echo "selected = \"selected\"";
你需要在这里这样做:
$ops .= "<option value='" . $productid1 . "'";
if($_POST['products']==$productid1) {$ops .= "selected = 'selected'";}
$ops .= ">" . $rank . "" . $period . "" . $name1 . "</option>";
答案 1 :(得分:0)
试试这个:
<?php
$sessionid = $_SESSION['uid'];
$period = ". ";
$info = $conn->prepare("SELECT `productid`,`name` FROM `products` WHERE id = :id ORDER BY `name` DESC ");
$info ->bindParam(':id', $sessionid , PDO::PARAM_INT);
$info->execute();
$rank = 0;
$last_score = false;
$rows = 0;
$ops = '';
while ($userinfo = $info->fetchobject()) {
$rows++;
if( $last_score!= $userinfo->name ){
$last_score = $userinfo->name;
$rank++;
}
$productid1 = "$userinfo->productid";
$name1 = "$userinfo->name";
if($_POST['products']== $productid1){
$sel = "selected = 'selected'";
}
$ops.= "<option value='" . $productid1 . "'". $sel . ">" . $rank . "" . $period . "" . $name1 . "</option>";
}
&GT;