Problem with PHP & Drop down boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bablumm
Forum Newbie
Posts: 1
Joined: Tue Jan 30, 2007 12:44 pm

Problem with PHP & Drop down boxes

Post 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]
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post 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>
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post 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>
Last edited by jyhm on Wed Jan 31, 2007 6:47 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Remember to close each option container.. ;)
Post Reply