Dymanic Dropdown List

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
bass_avb
Forum Newbie
Posts: 3
Joined: Thu Oct 15, 2009 2:17 am

Dymanic Dropdown List

Post by bass_avb »

Hey all,

I have two tables menu and pages. I am trying to create a dropdown list which pre-selects the page based on id of the page. I created page_id in my menu table which corresponds to id of the page from pages table. Here is the visual of what I want:

Code: Select all

 
Menu Item              Pages
       |                           |
Home Page ==> [Select Page]
                    [Home Page] *
                    [Bio]
                    [Info]
                    [Contacts]
HTML Output:

Code: Select all

<form>
<select>
      <option selected value"1">Home Page</option>
      <option value"2">Bio</option>
      <option value"3">Info</option>
      <option value"4">Contacts</option>
</select>
</form>
This is the code I have so far:

Code: Select all

 
function drop_down_pages(){
global $connection;
$db=mysql_select_db("content_MS", $connection);
$sql=mysql_query("SELECT page_name, id FROM pages ORDER BY page_name");
$pid=mysql_fetch_array($sql);
$sql2=mysql_query("SELECT page_id FROM menu ORDER BY page_id");
$mid=mysql_fetch_array($sql2);
$output ="<form>";
$output.="<select>";
if($mid['page_id']==$pid['id']){
   while($row=mysql_fetch_array($sql)){
   $page_name=$row['page_name'];
   $id=$row['id'];
   $output.="<option selected value=\"$id\">$page_name</option>";
    }
}
else{
   while($row=mysql_fetch_array($sql)){
   $page_name=$row['page_name'];
   $id=$row['id'];
   $output.="<option value=\"$id\">$page_name</option>";
    }
}
$output.="</select>";
$output.="</form>";
return $output;}
When I test the code all I get is a list of pages with the first one selected but it doesn't match.

Any ideas?
Post Reply