Jump to content

Dynamic Dropdown List


bass_avb

Recommended Posts

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:

 

 
Menu Item              Pages
       |                           |
Home Page ==> [select Page]
                    [Home Page] *
                    [bio]
                    [info]
                    [Contacts]

HTML Output:

<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:

 
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?

 

Link to comment
Share on other sites

your code should be

 

 

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>";
   while($row=mysql_fetch_array($sql)){
   $selected = '';
   $page_name=$row['page_name'];
   $id=$row['id'];
   if ($mid['page_id']==$pid['id']) $selected = " selected='selected' ";
   $output.="<option value=\"$id\" {$selected}>$page_name</option>";
    }
$output.="</select>";
$output.="</form>";
return $output;}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.