kaitsukiyo
kaitsukiyo

Reputation: 9

How do I prioritize one of the option in a drop down menu

I am having a list of Category:

cat1
cat2
cat3
cat4
.
.
.
cat8

and I displaying it with this code:

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

?>

I am trying to using this list to displaying the data in a database in mysql. What am I know want to do is when I want to display out the data from mysql, I displayed using the list in a dropdown menu using code at above, but I don't know how to display out the list if my stored data is not "cat1", it might be cat4 or cat 6 for example. How am I actually can do to display the particular line according to what i stored in mysql?

Upvotes: 1

Views: 109

Answers (1)

rain
rain

Reputation: 243

You can try this code:

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;
}

Upvotes: 1

Related Questions