Jump to content

Populating Drop Down from database


herghost

Recommended Posts

Hi All,

 

I have a table called featurelist which contains about 8 fields, however I just want to grab 2, id and feature_name and display these on a dropdown list in another form.

 

So Basically:

 

This products features:

<option>1.Red</option>

<option>2.Blue</option>

 

etc..

 

However what I want saved into the new database is just the id.

 

How would I go about this?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/194989-populating-drop-down-from-database/
Share on other sites

Your PHP would need to look something like:

 

// run your query

while ($row = mysql_fetch_assoc($query))
{
    echo '<option value="{$row['id']}">{$row['feature_name']}</option>';
}

 

That way the value attribute contains the ID and once the form is submitted that ID will be passed on.

Thanks,

 

However I have this:

 

<?php 
						$query = "SELECT * FROM product_features";
						$result = mysql_query($query);
						while ($list = mysql_fetch_assoc($result)) {

				echo '<dt><label for="product_features_id">Features Package:</label></dt>';
                    echo '<dd><select size="1" name="product_features_id">';
				echo '<option value="';
				echo $list['id'];
				echo '">';
				echo $list['id'];
				echo ' ';
				echo $list['name'];
				echo '</option>';
				echo '</select>';
				}
				?>              

 

As there is more than one result in the database, it is creating a dropdown box for each value instead of putting all values in one dropdown box?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.