Select Dropdown with info from DB

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
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Select Dropdown with info from DB

Post by Cooperman »

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>
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Select Dropdown with info from DB

Post by Frozenlight777 »

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

Re: Select Dropdown with info from DB

Post by Cooperman »

Thanks.

I get the first name but it doesn't go through the list in the DB.

Any ideas?
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Select Dropdown with info from DB

Post by Frozenlight777 »

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

Re: Select Dropdown with info from DB

Post by nowaydown1 »

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

Re: Select Dropdown with info from DB

Post by Cooperman »

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

Re: Select Dropdown with info from DB

Post by Cooperman »

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

Re: Select Dropdown with info from DB

Post by nowaydown1 »

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.
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Select Dropdown with info from DB

Post by Frozenlight777 »

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

Re: Select Dropdown with info from DB

Post by Cooperman »

Thanks guys, it now works.

I appreciate it :D
Post Reply