Dynamically populating a drop down list via PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
colafran
Forum Newbie
Posts: 1
Joined: Thu Mar 13, 2008 2:08 pm

Dynamically populating a drop down list via PHP

Post by colafran »

I am new to PHP, bear with me :)

I am looking to populate a drop down list by doing a MySQL query using PHP. I found a code example, but it does not seem to work, can anyone tell me if there is something wrong with it? The posting I saw was from 2005, so I am wondering if the code is incompatible with PHP5?

Before you ask, I know that my database query is working, I cut and pasted it from another script that is working.

Another thing, the drop down does show 5 spaces, which it should be because the sql query is returning only 5 records, but the spaces are blank. I attached a screen cap of it.
ddown.JPG
ddown.JPG (3.08 KiB) Viewed 347 times

This is the exact code I have:


<?php
// Connect and select a database
mysql_connect("localhost", "root", "");
mysql_select_db("addressBook");

// Run a query
$result = mysql_query("SELECT * FROM colleague");
?>

<!- HTML form opening tags -->
<form action="action" method="post">
<select name="option">

<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[value]\">$row[title]</option>\n";
}
?>

<!- HTML form closing tags -->
</select>
<input type="submit">
</form>
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Dynamically populating a drop down list via PHP

Post by kryles »

$row[value] $row[title]

im not sure but wouldnt that be if you made your sql like

Code: Select all

 
 
$result=("SELECT value, title FROM tblname");
 
 
I usually list the fields so I'm not sure this way
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Dynamically populating a drop down list via PHP

Post by flying_circus »

Hey Colafran, looks like you are missing some single quotes. Try this:

Code: Select all

<?php
//for each row we get from mysql, echo a form input
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['value'] . '">' . $row['title'] . '</option>\n';
}
?>
The reason you are getting 5 blanks is because you need to wrap the array key in quotes. $row[value] is not the same as $row['value'];
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Dynamically populating a drop down list via PHP

Post by flying_circus »

kryles wrote:$row[value] $row[title]

im not sure but wouldnt that be if you made your sql like

Code: Select all

 
 
$result=("SELECT value, title FROM tblname");
 
 
I usually list the fields so I'm not sure this way
Explicitly defining the values you want in your SQL statement is not wrong, it just wont solve this problem. I generally do the same as you unless I need alot of fields from the database, then lazyness typically takes over and I use the wildcard *.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Dynamically populating a drop down list via PHP

Post by kryles »

okay so behind the scenes PHP will recognize field names and when called $row['fieldName'] it'll know what you mean even when using asterix.....something new for me lol thanks :)
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Dynamically populating a drop down list via PHP

Post by flying_circus »

kryles wrote:okay so behind the scenes PHP will recognize field names and when called $row['fieldName'] it'll know what you mean even when using asterix.....something new for me lol thanks :)
Exactly! No problem :)
Post Reply