itsrien Posted February 19, 2008 Share Posted February 19, 2008 I have a question about Dynamic Select Menus With PHP. De following I like to getting work: I have two tables: one is categories the other is named links. Now I have a edit page so I can edit the links. On that edit page I have a php pull down menu where I can change the category. This already working, the only problem is that it doesn't remember the last option I set. When I reload the page it always shows the first (default) option, from the selection list. How do I change the script, so the pull down menu remembers the last set option or load the right value from the database. Bellow I attached the code: Thanks in advance: Rien //SELECT AND EDIT CATEGORY $sql = "SELECT * FROM tb_links"; $link_query = mysql_query($sql); $link = mysql_fetch_array($link_query); $sql = "SELECT * FROM tb_categories ORDER BY catname"; $category_query = mysql_query($sql); echo '<select name="category">'; while( $category = mysql_fetch_array($category_query) ){ if( $link['catparent'] == $category['ID'] ){ $selected = 'selected="selected"'; } echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>'; } echo '</select><br>'; //SELECT AND EDIT CATEGORY Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/ Share on other sites More sharing options...
itsrien Posted February 23, 2008 Author Share Posted February 23, 2008 No body? Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/#findComment-474546 Share on other sites More sharing options...
revraz Posted February 23, 2008 Share Posted February 23, 2008 Write the last option set to the database? Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/#findComment-474547 Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 Looks like what you were trying to achieve here: if( $link['catparent'] == $category['ID'] ){ $selected = 'selected="selected"'; } Your problem may lie in the fact that there's no space after the value and before the word selected. Try: if( $link['catparent'] == $category['ID'] ){ $selected = ' selected="selected"'; } Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/#findComment-474551 Share on other sites More sharing options...
itsrien Posted February 23, 2008 Author Share Posted February 23, 2008 Thank you so much for the answers already, but unfortunately the change didn't bring me the result I had hoped for. The result is still posted correctly to the database, the only thing still not working is that the edit page pull down menu not showing the right value. Every time the page reload, it shows the default value. So for example: I have a link in the database called url: http://sports.com title: sports catname: sport When I submit the page, the data is stored correctly in de database. But when I reload the edit page again, it doesn't load the right value from the database. And only the default value (in this case it is cars) is showed. I can choose the right value, but I would like the correct value is showed. Bellow an example of the tables in the database: Table: links ID url title catparent 1 http:// buddy 3 2 http:// sports 2 3 http:// cars 1 Table: categories ID catname 1 cars 2 sport 3 friends The field catname from de table categories, contained the desired values for the pulldown menu. Bellow I attached the changed code, what do I need to change more to get it work? //SELECT AND EDIT CATEGORY $sql = "SELECT * FROM links"; $link_query = mysql_query($sql); $link = mysql_fetch_array($link_query); $sql = "SELECT * FROM categories ORDER BY catname"; $category_query = mysql_query($sql); echo '<select name="category">'; while( $category = mysql_fetch_array($category_query) ){ if( $link['catparent'] == $category['ID'] ){ $selected = ' selected="selected"'; } echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>'; } echo '</select><br>'; //SELECT AND EDIT CATEGORY Thanks in advance: Rien Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/#findComment-474710 Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 Well your code makes no sense to me. You're pulling the first entry from the links table (rather than linking through), and you don't have a where clause or anything. But from the second table, you're pulling everything, and you're checking each one of them against the first link. That's what your code does. Additionally, let's say that they do match on the first iteration (i.e. the first result returned from the second table matches the result from the first table) all of the other 3 selections will be marked "selected" as well, because you don't clear the $selected variable at the end of each loop. If the selected variable is set on the first iteration, it'll remain set on the second, third, fourth, and so on. Add an unset($selected) or $selected = '' to the end of the loop, and explain the first part of the code to me, and maybe we can shed some light on this. Quote Link to comment https://forums.phpfreaks.com/topic/91834-howto-dynamic-select-menus-with-php-and-remembered-last-option-set/#findComment-474757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.