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
Cooperman
Forum Commoner
Posts: 27 Joined: Mon Jun 02, 2008 4:44 am
Post
by Cooperman » Wed Jun 04, 2008 8:22 am
Hi there,
Can anyone tell me how I populate the Select Dropdown menu with Names from the database I have?
I think I need a loop going on to pick up all the names. I have a row in the DB called "user" and I wish to populate the letters "a" and "b" in the Select dropdown below.
Many thanks.
HTML______________________________________
<tr>
<td width="127">Select Name:</td>
<td width="127"><select>
<option value="a">"b"</option>
</select></td>
</tr>
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Jun 04, 2008 8:30 am
Experiment with that a little. I have no idea if it'll work... you'll have to twink it quite a bit
Code: Select all
$sql = "SELECT dropdownitem FROM table;
$result = mysql_query($sql, $db);
$myrow = mysql_fetch_array($result);
$total_elements = count($myrow['dropdownitem']);
for ($i=0;$i<$total_elements;$i++) {
echo "<select><option value=" . $myrow[$i] . ">" . $myrow[$i]. "</option></select>";
}
Cooperman
Forum Commoner
Posts: 27 Joined: Mon Jun 02, 2008 4:44 am
Post
by Cooperman » Wed Jun 04, 2008 8:48 am
Thanks.
I get the first name but it doesn't go through the list in the DB.
Any ideas?
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Jun 04, 2008 8:57 am
I might have messed up the array then. I'll check it out...
nowaydown1
Forum Contributor
Posts: 169 Joined: Sun Apr 27, 2008 1:22 am
Post
by nowaydown1 » Wed Jun 04, 2008 9:01 am
The reason Frozen's example only shows you one is because you need to loop over the result set with mysql_fetch_array:
Code: Select all
$sql = "SELECT dropdownitem FROM table";
$result = mysql_query($sql, $db);
while($myrow = mysql_fetch_array($result)) {
echo "<select><option value=" . $myrow["drowndownitem"] . ">" . $myrow["downdownitem"]. "</option></select>";
}
The single call to mysql_fetch_array will only give you the first row in the result set. Same idea that Frozen gave you, just a little bit different on the looping side.
Cooperman
Forum Commoner
Posts: 27 Joined: Mon Jun 02, 2008 4:44 am
Post
by Cooperman » Wed Jun 04, 2008 9:07 am
OK, this now gives me lots of dropdowns with only one name in them. I think it is looping the select tag.
$sql = ("SELECT firstName FROM login order by firstName");
$result = mysql_query($sql, $link);
while($myrow = mysql_fetch_array($result)) {
echo "<option value=" . $myrow["firstName"] . ">" . $myrow["firstName"]. "</option></select>";
}
Cooperman
Forum Commoner
Posts: 27 Joined: Mon Jun 02, 2008 4:44 am
Post
by Cooperman » Wed Jun 04, 2008 9:09 am
Sorry, missed out a select tag above.
$sql = ("SELECT firstName FROM login order by firstName");
$result = mysql_query($sql, $link);
while($myrow = mysql_fetch_array($result)) {
echo "<select><option value=" . $myrow["firstName"] . ">" . $myrow["firstName"]. "</option></select>";
}
nowaydown1
Forum Contributor
Posts: 169 Joined: Sun Apr 27, 2008 1:22 am
Post
by nowaydown1 » Wed Jun 04, 2008 9:11 am
Yeah, that's my fault. Sorry I didn't see those select tags inline there.
Code: Select all
$sql = "SELECT dropdownitem FROM table";
$result = mysql_query($sql, $db);
echo "<select name=\"myDropdown\">";
while($myrow = mysql_fetch_array($result)) {
echo "<option value=" . $myrow["drowndownitem"] . ">" . $myrow["downdownitem"]. "</option>";
}
echo "</select>";
Is more what you want.
Frozenlight777
Forum Commoner
Posts: 75 Joined: Wed May 28, 2008 12:59 pm
Post
by Frozenlight777 » Wed Jun 04, 2008 9:11 am
echo "<tr><td><select name=items>";
for ($i = 0; $i < $total_elements; $i++){
echo " <option = " . $myrow1[$i] . ">" . $myrow1[$i] . "</option>";
}
echo "</td></tr>";
sorry, don't loop the select in it
Cooperman
Forum Commoner
Posts: 27 Joined: Mon Jun 02, 2008 4:44 am
Post
by Cooperman » Wed Jun 04, 2008 9:19 am
Thanks guys, it now works.
I appreciate it