Page 1 of 1

html dropdown list in php from mysql

Posted: Sun Oct 18, 2009 10:09 pm
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?

Re: html dropdown list in php from mysql

Posted: Sun Oct 18, 2009 10:19 pm
by guosheng1987
print "<br> option list is " . $options;

you means this above not be output?

Re: html dropdown list in php from mysql

Posted: Sun Oct 18, 2009 10:40 pm
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...

Re: html dropdown list in php from mysql

Posted: Mon Oct 19, 2009 10:50 pm
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"...

Re: html dropdown list in php from mysql

Posted: Tue Oct 20, 2009 6:16 am
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