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?
html dropdown list in php from mysql
Moderator: General Moderators
-
guosheng1987
- Forum Newbie
- Posts: 24
- Joined: Thu Oct 15, 2009 3:03 am
Re: html dropdown list in php from mysql
print "<br> option list is " . $options;
you means this above not be output?
you means this above not be output?
Re: html dropdown list in php from mysql
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...
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
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"...
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
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
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