Page 1 of 1

Problem with PHP & Drop down boxes

Posted: Tue Jan 30, 2007 12:59 pm
by bablumm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am new to PHP and programming. I am trying to retrieve information from a mySQL database to display in a single drop down box for the user to select from. With the code below, I retrieve the information, but each row displays separately in its own drop down box! Any information you could supply would be very much appreciated. Thanks in advance.

Code: Select all

<html>
<body>
<?php
@ $db = mysql_pconnect('localhost', 'formuser', 
'form123db');
mysql_select_db('documents');




$result = mysql_query("SELECT ID, typename FROM typesform");

while($row = mysql_fetch_array($result))
{
   
echo "Select Citation Type:";
   echo "<select> size=40 <option value=" . $row['ID'] . ">" .
      $row['typename'] . "</option></select>";  


}
?>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 30, 2007 1:29 pm
by boo_lolly
this should get you started:

Code: Select all

<html>
<body>
<?php
	$db = mysql_pconnect('localhost', 'formuser', 'form123db');
	mysql_select_db('documents');

	$result = mysql_query("SELECT ID, typename FROM typesform");

	echo "<select name=\"dropdown\">\n";
	while($row = mysql_fetch_array($result)){
		echo "<option value="\". $row['ID'] ."\">". $row['typename'] ."\n";

	}
	echo "</select>\n";
?>
</body>
</html>

Posted: Tue Jan 30, 2007 2:14 pm
by jyhm
You can also add a dead option tag for a title if you want:

Code: Select all

<html>
<body>
<?php
        $db = mysql_pconnect('localhost', 'formuser', 'form123db');
        mysql_select_db('documents');

        $result = mysql_query("SELECT ID, typename FROM typesform");

        echo '<select name="dropdown">\n
               <option value="">Drop Title Here</option>'; // Dead option here for formatting.  
        
                while($row = mysql_fetch_array($result)){
                      echo '<option value="' . $row['ID']  . '">' . $row['typename']  . '</option>\n';
                }
        echo '</select>\n';
?>
</body>
</html>

Posted: Tue Jan 30, 2007 4:13 pm
by feyd
Remember to close each option container.. ;)