简体   繁体   中英

Pass A Value From Last Page And Auto Selected On The Dropdown List PHP

I am working on a project using PHP. Passed two parameters from previousp page to page Update.php :

$ID = $_GET['PosterId'] 
$catID = $_GET['CategoryID'];

On Update.php , a dropdown lists all categories retrieved from category table.

$sql = "SELECT PosterID, CategoryID, Category, Title, Price FROM tblposters where PosterID =" . $ID;    
$sqlCategory = "select  CategoryID, CategoryName from tblcategory";

$result = mysqli_query($conn, $sqlCategory);
$resultCategory = mysqli_query($conn, $sqlCategory);

// get category list from table
echo "<select id='Category' name='CategoryList'>";
echo "<option value='-1'>Please Select Posters Group</option>";

while( $row =  $resultCategory -> fetch_assoc( )) {     
    $catRowID = $row['CategoryID'];
    $cat = $row['CategoryName'];
    echo "<option value='$catRowID'> $cat </option>";
}

echo "</select></div>"; 
?>

Since the categoryID for this PosterID has been passed to this page, I want to display it's categoryname by default in the dropdown. How could I write the code with PHP on the <option> ?

Thanks.

Try this way:

$sql = "SELECT PosterID, CategoryID, Category, Title, Price FROM tblposters where PosterID =" . $ID;    
$sqlCategory = "select  CategoryID, CategoryName from tblcategory";

$result = mysqli_query($conn, $sqlCategory);
$resultCategory = mysqli_query($conn, $sqlCategory);

//get category list from table
echo "<select id='Category' name='CategoryList'>";
echo "<option value='-1'>Please Select Posters Group</option>";
while( $row =  $resultCategory -> fetch_assoc( )){  
$catRowID = $row['CategoryID'];
$cat = $row['CategoryName'];
if($_GET['CategoryID'] == $row['CategoryID'])
{
echo "<option value='$catRowID' selected> $cat </option>";
}
else
{
echo "<option value='$catRowID'> $cat </option>";
}
}
echo "</select></div>"; 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM