Jump to content

Drop Down Menu Takes Values From Database


fazlionline

Recommended Posts

Of course it's possible. A single option for a dropdown menu needs a name and a value, so as long as you have some table in a database that has 2 fields in it (or only one if you want to use the same value for both), you can get the records and use a loop to print out the options.

Link to comment
Share on other sites

I'm no coder but I think I've seen something like this

echo "<select>";foreach ($brandName as $brandName)  echo "<option name="brand" value="$brandID">$brandName</option>";endforeach;echo "</select>";

or maybe it's ($row as $brandName)..So every single option has it's own name and unique value (id)..

Link to comment
Share on other sites

but what is that "single option"
This is a single option:<option value="the value">the label</option>It has a label, and a value. If you get your labels and values from the database you can use a loop similar to the one webtrix posted to write each option. Do you have a database set up? Do you have code to get data from the database?
Link to comment
Share on other sites

You think this will work<option value="the value">the label</option>To change it like<option value="http://www.yahoo.com">Yahoo</option>Till now I do not have a database, I can work with MySQL database, but I am beginner. I want to know the code how to put these values in database, and other code how to get values from database in PHP.My code in PHP is

<table><tr> <td><select name="favs1" onchange="window.open(this.options[this.selectedIndex].value)"><option>Selct a link</option><option value="
value="
value="
value="
value="

Is this code right?

Link to comment
Share on other sites

In order to put the values in the database you need to either use a database admin system, like phpMyAdmin, where you can create your table and insert records into it, or you need to build a form and a processing script. The form contains the fields that go in the database, whatever you want them to be (possibly just a URL and title), and the processing script just needs to get the values and use an insert statement to add to the database. If you're building a form to insert things, you'll also want to build a form to edit or delete things already there.If you have something like phpMyAdmin installed on the server, I suggest you look there first. You can already do all of this through phpMyAdmin. If you need to build the forms yourself, look at the PHP Forms tutorial on the w3schools site for information about that.http://www.w3schools.com/php/php_forms.aspThe section about using PHP with MySQL will tell you about how to connect to the database server with your username and password, select your database, and use it.http://www.w3schools.com/php/php_mysql_intro.aspTo display the entries from the database, assuming you are already connected to the database, you would just need something like this:

$result = mysql_query('SELECT label, val FROM links');while ($row = mysql_fetch_assoc($result)){  echo '<option value="' . $row['val'] . '">' . $row['label'] . '</option>';}

That assumes that you have a table in the database called "links" with fields called "label" and "val".

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...