bass_avb Posted December 3, 2009 Share Posted December 3, 2009 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 https://forums.phpfreaks.com/topic/183830-dynamic-dropdown-list/ Share on other sites More sharing options...
rajivgonsalves Posted December 3, 2009 Share Posted December 3, 2009 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 https://forums.phpfreaks.com/topic/183830-dynamic-dropdown-list/#findComment-970332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.