bschultz Posted July 22, 2011 Share Posted July 22, 2011 I have a page that allows a user to edit mysql data. It displays the current info in a form text field...but I want to switch that to a select box...with the current info "selected" in the form. I know that I can put "selected" in the form...but is there a better way than having almost 100 if statements (it's for the time...every 15 minutes would be the select options...so 24x4)? <option value="01:23:00" selected>TBA</option> <option value="00:00:00">12:00am</option> <option value="00:15:00">12:15am</option> <option value="00:30:00" <?php if ($row[time] = "00:30:00") { echo "selected='00:30:00' } ?>>12:30am</option> <option value="00:45:00">12:45am</option> This would select 12:30am...but like I said, is there a way to do this WITHOUT an if statement for each value? Quote Link to comment https://forums.phpfreaks.com/topic/242605-form-select-box-with-selected-entry-based-on-db-query/ Share on other sites More sharing options...
Andrew777 Posted July 22, 2011 Share Posted July 22, 2011 Try this man... <form> <?php $temp = $row['time'];?> <select name="time" id="time"> <option value="" selected="selected"></option> <option value="00:00:00" <?php selrec($temp,"00:00:00"); ?>>12:00am</option> <option value="00:15:00" <?php selrec($temp,"00:15:00"); ?>>12:15am</option> <option value="00:30:00" <?php selrec($temp,"00:30:00"); ?>>12:30am</option> etc.............. </select> </form> <?php function selrec($temp,$val) { if($temp==$val) { echo "selected"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242605-form-select-box-with-selected-entry-based-on-db-query/#findComment-1246049 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.