I am using the following query to pull categories from the DB then creating a select list.

Can some tell me how to make the current value selected in the list when they open the category?

<?php
include("mysql.php");
$queryc = "select * from tbl_category WHERE active = 'yes' ORDER by name ASC";
$resultc = mysql_query($queryc);
while ($rowc = mysql_fetch_array($resultc)) {
$catIDc = $rowc;
$namec = $rowc;
$activec = $rowc;

echo "<option value=$catIDc>$namec</option>";
}
?>

Recommended Answers

All 4 Replies

I am not quite sure what you are wanting here.

Remember you can only select one item from a drop down by default and since you outputing all that are equal to yes I get confused as to the purpose of the while loop (unless there are many rows...which wouldnt work).

To set the default selected item on a drop down do it like so...

<option value="..." selected="selected"></option>

Could you offer more of an explantion?

-------------------------------------------------------------------

Website Tutorials

<?php
//connection
//select db
$selected_id=$_POST['select'];
$query="Select * from table";
$result=mysql_query($query);
$options="";
while($row=mysql_fetch_array($result)){
   $name=$row['name'];
   $id=$row['id'];
   if($selected_id==$id){ 
          $selected="selected": 
    }
   else {
      $selected=""; 
   }
   $options.="<option value='$id' $selected>$name</option>";
}
?>
<html>
<body>
<form method="post" action="test.php">
Select something: <select name="select">
<?php echo $options; ?>
</select><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Cheers,
Naveen

commented: Great answers. +1

Worked great, thanks.

You are welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.