0

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

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by nav33n
Featured Replies
  • 1

    [code=php] <?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> [/code] … Read More

0

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

1
<?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

Comments
Great answers.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.