View Full Version : Using PHP and mySQL to populate listbox
| My experience with PHP until this point was with "PHP only" scripts (no HTML included in code). I've now built my page in Dreamweaver so my form looks pretty, but I guess pretty doesn't mean much if I don't know how to make it work. So, I think I need someone's help.
I have 2 questions regarding PHP's interaction with forms.
On my page I have 3 radio buttons (first,after,last)
Next to the "after" radio button I have a drop-down listbox (after_list)
Maybe my first question doesn't involve PHP but how do I go about making the "after" radio button select the "after_list" listbox?
My second question regards embeding PHP into HTML. How would I write the PHP code into my Dreamweaver HTML to querry the mySQL server and populate the listbox with a columns' information?
Any help would be greatly appreciated.
Thanks,
JOSH |
this is how i have done it:
<td colspan="2" class="BLACK">
<select name="devicetypeid">
<?php
$sql2 = "SELECT *
FROM DEVICETYPE
ORDER BY DESCRIPTION";
$result2=mysql_query($sql2,$dbcnx);
while ( $dropdown = mysql_fetch_array($result2) ){
if ($dropdown["DEVICETYPEID"]==$devicetypeid){
echo("<option selected value=". $dropdown["DEVICETYPEID"]. ">" . $dropdown["DESCRIPTION"] . "</option>");
}
else {
echo("<option value=". $dropdown["DEVICETYPEID"]. ">" . $dropdown["DESCRIPTION"] . "</option>");
}
}
?>
</select>
</td>
| If you're using it more than one location then build a function and just call the function in your page.
function def:
function optionBlockRecipe()
{
$dbConn = new MyConnect();
$sql = "SELECT recipe_id, menu_name FROM menu_item ORDER BY menu_name";
$rs = mysql_query($sql, $dbConn->conn)or die("Connection to DataBase failed");
for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print("<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[1]</OPTION>\n");
}
}
call anywhere by inserting in php tags( except if you're already within the php tags...;))
<?php optionBlockRecipe(); ?> |
vBulletin v3.5.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.