Jump to content

html forms: showing sql result in select option list


bobocheez

Recommended Posts

How can the following code be altered so that the select options menu shows all of the subcat choices?

 

This is just a form that queries the database first

 

<?php
include('config.php');

$query = "SELECT * FROM subcategories ORDER BY cat_id ASC";
$result = mysql_query($query) or die('Sorry, could not access the database');
mysql_num_rows($result);
$subcat_id = $row['subcat_id'];
$subcat = $row['subcategorie_name'];
$cat_id = $row['cat_id'];

?>
<form action="addchan2.php" method="post">
<select name="subcat">
<?php while($row=mysql_fetch_array($result, MYSQL_ASSOC))
   {
       $cat = $row['cat'];
   	echo "<option value=\"$subcat\">$subcat</option>";
   } ?>
</select>
<input type="text" name="chan">
<input type="text" name="subcat_id" value="<?echo"$subcat_id";?>">
<input type="text" name="cat_id" value="<?echo"$cat_id";?>">
<input type="submit" value="Add">
</form>

Link to comment
Share on other sites

<select name="subcategory">
    <optgroup label="category1">
        <option value="subcategory1">Subcategory #1</option>
        <option value="subcategory2">Subcategory #2</option>
    </optgroup>
</select>

 

Remember that you can't create a optgroup inside a option therefor you can only go as deep as multiple subcategories for a category. Subcategories can not on their turn contain Sub-Subcategories.

 

Note: optgroup is not selectable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.