2014-06-13 48 views
-1

我對產品的價值進行排名並將其列在下拉菜單中。我似乎無法保留從下拉菜單中選擇的排名值。任何幫助將不勝感激,在此先感謝。保留從下拉框中選擇的排名值

<?php 

$sessionid = $_SESSION['uid']; 

$period = ".&nbsp"; 

$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

這樣可不行,你就會明白:

if($_POST['products']=='$ops') echo "selected = \"selected\""; 

你需要做的是在這裏:

$ops .= "<option value='" . $productid1 . "'"; 
if($_POST['products']==$productid1) {$ops .= "selected = 'selected'";} 
$ops .= ">" . $rank . "" . $period . "" . $name1 . "</option>"; 
+0

這個工作。非常感謝 :) – user3595005

0

試試這個:

<?php 

$sessionid = $_SESSION['uid']; 

$period = ".&nbsp"; 

$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>"; 

}   

>