newb Posted October 9, 2006 Share Posted October 9, 2006 [code]<?php $query = $config->query("SELECT * FROM table_modules"); echo "<select name='conf_default_mod'>"; while ($row = mysql_fetch_array($query)) { echo "\n<option value='$row[id]'>$row[name]</option>\n"; } echo " </select> ?>[/code]how do i change the code so that it echo's whatever option is selected in the database. like selected="selected" . any one know how? Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/ Share on other sites More sharing options...
Daniel0 Posted October 9, 2006 Share Posted October 9, 2006 Inside the loop: [code]$selected = $row['is_selected'] ? " selected='selected'" : null;echo "\n<option value='$row['id']'{$selected}>$row['name']</option>\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/#findComment-106418 Share on other sites More sharing options...
newb Posted October 9, 2006 Author Share Posted October 9, 2006 didnt work., i dont have a $row'is_selected' field in my table. Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/#findComment-106423 Share on other sites More sharing options...
Daniel0 Posted October 9, 2006 Share Posted October 9, 2006 No, but use what corresponds to your database structure. Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/#findComment-106426 Share on other sites More sharing options...
newb Posted October 9, 2006 Author Share Posted October 9, 2006 ah i got it to work :D Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/#findComment-106429 Share on other sites More sharing options...
newb Posted October 9, 2006 Author Share Posted October 9, 2006 [code]<?php $query = $config->query("SELECT * FROM table_modules"); echo "<select name='conf_default_mod'>"; while ($row = mysql_fetch_array($query)) { $option = "\n<option value='$row[id]'>$row[name]</option>\n"; $query2 = $config->query("SELECT * FROM table_settings WHERE id='11'"); $row2 = mysql_fetch_array($query2); $def_mod = $row2['value']; if(eregi("$def_mod",$option)) { $option = str_replace($option,"\n<option value='$row[id]' selected='selected'>$row[name]</option>\n",$option); } else { $option = "\n<option value='$row[id]'>$row[name]</option>\n"; } echo $option; } echo " </select>";?>[/code]incase anyone wanted to know how it was done Quote Link to comment https://forums.phpfreaks.com/topic/23457-change-choice-to-whatever-is-selected/#findComment-106431 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.