Page 1 of 1

Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 8:22 am
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>

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 8:30 am
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>";
}
 
 

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 8:48 am
by Cooperman
Thanks.

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

Any ideas?

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 8:57 am
by Frozenlight777
I might have messed up the array then. I'll check it out...

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:01 am
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.

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:07 am
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>";

}

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:09 am
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>";

}

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:11 am
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.

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:11 am
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

Re: Select Dropdown with info from DB

Posted: Wed Jun 04, 2008 9:19 am
by Cooperman
Thanks guys, it now works.

I appreciate it :D