Author Topic: printing values to a drop down box  (Read 299 times)

0 Members and 1 Guest are viewing this topic.

Offline woodpleaseTopic starter

  • Enthusiast
  • Posts: 111
    • View Profile
printing values to a drop down box
« on: September 02, 2010, 05:19:23 PM »
i'm trying to print out values from a table into a drop down box, but my code doesnt seem to work. when i test it, there are no values in the drop down box.

if someone could find the problem, that would be great.

<form name="add_sub_section" method="post" action="<?php echo $_SERVER['../PHP_SELF']; ?>">
Sub Section Name <input type="text" name="sub_section_title" maxlength="200" /><br/><br/>
Sub Section desc <textarea name="sub_section_desc" rows="4" columns="30" /></textarea><br/><br/>
SECTION 
<?php
$list 
"SELECT section_title FROM section_main";
echo 
"<select name='section_title'>";
while (
$a mysql_fetch_row($list)) {
for (
$j 0$j mysql_num_fields($list); $j++) {
echo 
"<option value="$a[$j] . ">"$a[$j] . "</option>";
}
}
echo 
"</select>";
?>
<br/><br/>
<input type="submit" name="submit" value="Add Section"/>
</form>


the connection to the table works, so i havnt put that code in.
 there's no formatting for the html yet, i just want to get the php working first
Thanks
« Last Edit: September 02, 2010, 05:29:06 PM by woodplease »

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,753
    • View Profile
Re: printing values to a drop down box
« Reply #1 on: September 02, 2010, 05:40:04 PM »
You are not running the query. Odd, since I would think you would get an error when trying to run mysql_fetch_row() on a string. Plus, the logic of reading the records is all wrong.

Try this
Code: [Select]
<?php
 
$query 
"SELECT section_title FROM section_main";
$result mysql_query($query) or die(mysql_error());
$titleOptions '';
while (
$row mysql_fetch_assoc($query))
{
    
$titleOptions .= "<option value=\"{$row['section_title']}\">{$row['section_title']}</option>\n";
}
 
?>

 
<form name="add_sub_section" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Sub Section Name <input type="text" name="sub_section_title" maxlength="200" />
<br /><br />
Sub Section desc <textarea name="sub_section_desc" rows="4" columns="30" /></textarea>
<br /><br />
SECTION
<select name="section_title">
<?php echo $titleOptions?>
</select>
<br/><br/>
<input type="submit" name="submit" value="Add Section" />
</form>
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net