How to display results of SQL Query as a dropdown menu?

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
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

How to display results of SQL Query as a dropdown menu?

Post by nishmgopal »

Hey Guys, me again!

I have got the following code, it seems to work, but the only problem is, it displays two separate dropdown menus.

I would like both of the records to appear in the same drop down menu.

Can anyone help?

Code: Select all

$query = "SELECT * FROM Job_Spec";
   $result = mysql_query($query);
   
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
            $Project_Name= $row['Project_Name'];
    
 
  echo"
            <h1 class='title2'>Upcoming Project Roles</h1>
            <p>From the menu below please select the Project Role:</p>
            <form id='form2' method='post' action=''>
              <p>
                <label>
                <select name='job' id='job'>
                  <option value>'$Project_Name'></option>
                </select>
                </label>
              </p>
            </form>
";
}
?>
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: How to display results of SQL Query as a dropdown menu?

Post by ben.artiss »

Hi again, i think you just need to pull some stuff out of the loop:

Code: Select all

 
<?php
$query = "SELECT * FROM Job_Spec";
$result = mysql_query($query);
 
echo "
    <h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
    <form id='form2' method='post' action=''>
        <p>
            <label>
                <select name='job' id='job'>
";
 
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $Project_Name= $row['Project_Name'];
    echo "<option value>'$Project_Name'></option>";
}
 
echo "</select></label></p></form>";
?>
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: How to display results of SQL Query as a dropdown menu?

Post by nishmgopal »

Thank you so much! It worked!

I am sure I will be posting again very soon!

Thanks again.
Post Reply