html dropdown list in php from mysql

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
dkperez
Forum Commoner
Posts: 26
Joined: Fri Jun 26, 2009 9:41 am

html dropdown list in php from mysql

Post by dkperez »

OK, I found a bunch of examples of doing this, but I'm missing something. I want to create a dropdown list for a user to select a lastname from a list...

select id, lastname from mytable
this gets me 10 lastnames, each with a unique id.

$options = "";

loop and build the string of "option id" stuff...

while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$ln=$row["LastName"];

echo "<br> id = " . $id . " and " . $ln;

THIS echo above, works fine... I get each id and lastname perfectly. So apparently they are coming back from
the query, and are getting put in the local variables.....

$options .= "<option value =" . $id . ">" . $ln . "</option>\n";
print "<br> option list is " . $options;
}

I've tried echo and print. In both cases what I get is
option list is Allen Anderson Andrews Benson Bergmann Bergstrom Best Blocker

INSTEAD of
option list is <option value = "111">Allen</option>
<option value = "112">Anderson</option>
and so on.......

What is my php doing to me? And how do I make it NOT?
guosheng1987
Forum Newbie
Posts: 24
Joined: Thu Oct 15, 2009 3:03 am

Re: html dropdown list in php from mysql

Post by guosheng1987 »

print "<br> option list is " . $options;

you means this above not be output?
dkperez
Forum Commoner
Posts: 26
Joined: Fri Jun 26, 2009 9:41 am

Re: html dropdown list in php from mysql

Post by dkperez »

The print statement is just so I can see the set of options for the <select> being built.....

I've continued playing, and it APPEARS to be the "<" that's making things not work....

If I changr the line to
$options .= "option value =" . $id . ">" . $ln . "/option>\n";

I get

option list is option value =213>Allen/option> option value =339>Anderson/option> option value =331>Andrews/option>

which is close to what I need except there should be an "<" on front of "option value" and one in front of "/option"

I'm also not sure why the \n isn't causing a new line, but I figure it may be there and not showing in the echo or print...
dkperez
Forum Commoner
Posts: 26
Joined: Fri Jun 26, 2009 9:41 am

Re: html dropdown list in php from mysql

Post by dkperez »

Turn out the problem was whatever "htmlentities" fixes......

When I did it as echo htmlentities(anything with a <) it works fine......

I wish it hadn't taken me 4 - 5 hours of searching for an answer on the web, but at least this way I should remember it.....

And, yeah, a lot of the time, if what I'm supposed to be seeing is in the html I do a "source"...
jegan.aaodis
Forum Newbie
Posts: 15
Joined: Fri Oct 09, 2009 1:56 am

Re: html dropdown list in php from mysql

Post by jegan.aaodis »

Hi

Please use the below code .
This would helps you.

<select name="sel">
<option value="">Select name</option>
<?php
while ($row=mysql_fetch_array($result)) {
?>
<option value="<?php echo $row[name];?>"><?php echo $row[name];?></option>
<?php }?>
</select>


Thanks
Post Reply