View Full Version : Resolved Dropdown menu with PHP/MySQL
tabbie
05-02-2009, 02:09 AM
I'm relatively new to using MySQL in my PHP scripts, but I've almost got a site made with PHP/MYSQL but the only issue is I can't get my Drop down menu to show data from a MySQL table.
<select name="cat">
<?php
$cidv = $mem['cid'];
$name = $mem['name'];
$msel = mysql_query("select * from `ttt_cat`");
while ($mem = mysql_fetch_row($msel)) {echo "<option value=".$cidv.">".$name."</option>";} ?>
</select>
What I need the code to do is have it so that I can choose a category into which my Tutorial will be placed into. All that shows is the menu but the contents are blank. How can I get this to work?
Old Pedant
05-02-2009, 02:33 AM
You are trying to get the values from the records in the $mem row *before* you have even created the row.
The $mem['cid'] and $mem['name'] have to occur *inside* your while loop.
Minor note: you don't really need to assign those values to variables. You *can* use them directly in building the <option>.
Second minor note: Your HTML that results won't really be legit, esp. not if the page is XHTML. Your options will end up looking like
<option value=773>Adams</option>
missing the "..." around the value.
tabbie
05-02-2009, 03:35 AM
What do you mean create them inside the loop?
Old Pedant
05-02-2009, 05:23 AM
<select name="cat">
<?php
$msel = mysql_query("select * from `ttt_cat`");
while ($mem = mysql_fetch_row($msel))
{
$cidv = $mem['cid'];
$name = $mem['name'];
echo "<option value=".$cidv.">".$name."</option>";
}
?>
</select>
As I said, inside the loop. But I also said you don't really need them.
<select name="cat">
<?php
$msel = mysql_query("select * from `ttt_cat`");
while ($mem = mysql_fetch_row($msel))
{
echo "<option value=\"" . $mem['cid'] . "\">" . $mem['name'] . "</option>";
}
?>
</select>
Notice that I have now put the "..." in place that really should be there in your option:
<option value="xxx">yyyyy</option>
p.s.: I am *NOT* a PHP coder, so if I made a mistake in there, apologies. I assumed your code at least compiled and ran even if it ran incorrectly, in which case I would expect my changes to work.
tabbie
05-02-2009, 04:32 PM
Thanks for the help. In the script that was the only problem. :D
Edit: Okay, still not showing what I want. It's still a blank menu.
Old Pedant
05-02-2009, 10:51 PM
So *do* you have fields cid and name in that table???
Actually, you should have NOT used * in your SELECT, anyway.
Doing
$msel = mysql_query("select cid, name from ttt_cat");
would be much better.
Did you try the query in a MySQL tool??
tabbie
05-03-2009, 06:03 AM
So *do* you have fields cid and name in that table???
Actually, you should have NOT used * in your SELECT, anyway.
Doing
$msel = mysql_query("select cid, name from ttt_cat");
would be much better.
Did you try the query in a MySQL tool??
The query does work in phpMyAdmin. Changed it, but it doesn't do anything.
Killermud
05-03-2009, 03:03 PM
Try keeping the value variable within quotations as so : (Plus avoid using arrays inside quotations)
while ($mem = mysql_fetch_row($msel)){
$cidv = $mem['cid'];
$name = $mem['name'];
$options.="<option value=\"$cidv\">".$name.'</option>';
}
tabbie
05-03-2009, 05:38 PM
Okay, I got that to work. Thanks to all that helped. Here's the code I used to get it to work.
<select name="cat">
<?php
$msel = mysql_query("select * from ttt_cat");
while ($mem = mysql_fetch_array($msel)){
$cidv = $mem['cid'];
$name = $mem['name'];
$options.="<option value=\"$cidv\">".$name.'</option>';
}
print $options;
?></select>
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.