rickyjones Posted January 2, 2008 Share Posted January 2, 2008 Hello, I have an internal website that uses drop down menus to make categories for storing information. When you add a new item to the database you pick a category from the drop down list. Now when you edit the page I want the same drop down list created but I want to automatically select whichever category the user selected when they added the item to the database. Here is the function that I wrote to do this: /* Function_Categories_List() */ function Function_Categories_List() { // Import the issue_category variable. global $issue_category; // Create the SQL statement $sql = "SELECT * FROM categories"; // Execute the SQL statement $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { extract($row); if ($category_text == $issue_category) { echo("<option value=\"".$category_text."\" selected>$category_text</option>"); } echo("<option value=\"".$category_text."\">$category_text</option>"); } } It works however it shows an additional item in the dropdown box. Which category shows up as "selected" a second one is also displayed. I can understand /why/ it is displayed, but I cannot figure out how to delete that extra item. The end goal is for the program to read the assigned category for this item from the database. It then grabs all the available categories from the database. It presents this as a dropdown box and automatically selects the correct category. Thank you so much for your help! Sincerely, -Richard Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/ Share on other sites More sharing options...
revraz Posted January 2, 2008 Share Posted January 2, 2008 See if this works if ($category_text == $issue_category) { $selected="selected"; } echo("<option value='$category_text' $selected>$category_text</option>"); Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/#findComment-428277 Share on other sites More sharing options...
rickyjones Posted January 2, 2008 Author Share Posted January 2, 2008 See if this works if ($category_text == $issue_category) { $selected="selected"; } echo("<option value='$category_text' $selected>$category_text</option>"); Thank you! That worked. -Richard Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/#findComment-428292 Share on other sites More sharing options...
rickyjones Posted January 2, 2008 Author Share Posted January 2, 2008 Woops - I spoke too soon. That did remove the extra item but now all of the items in the drop down have the "selected" code attached to them, which means that the correct item is not selected by default. -Richard Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/#findComment-428302 Share on other sites More sharing options...
revraz Posted January 2, 2008 Share Posted January 2, 2008 if ($category_text == $issue_category) { $selected="selected"; } else { $selected=""; } Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/#findComment-428304 Share on other sites More sharing options...
rickyjones Posted January 2, 2008 Author Share Posted January 2, 2008 I apologize I did not get a chance to test the latest piece that you posted. I figured out a different way of displaying the data and having it work. Here is the function that I created: /* Function_Categories_List() */ function Function_Categories_List() { // Import the issue_category variable. global $issue_category; // Create the SQL statement $sql = "SELECT * FROM categories"; // Execute the SQL statement $result = mysql_query($sql); // How many rows are we looking at? $num_rows = mysql_num_rows($result); // For each row we need to get the data and check to see how to display it. Then display it! for ($i = 1; $i <= $num_rows; $i++) { $row = mysql_fetch_array($result); extract($row); if ($issue_category == $category_text) { echo("<option value=\"".$category_text."\" selected>$category_text</option>"); } else { echo("<option value=\"".$category_text."\">$category_text</option>"); } } } Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/84128-solved-drop-down-menu-default-selection-extra-item-shown/#findComment-428312 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.