cturner Posted January 22, 2007 Share Posted January 22, 2007 I am wondering how I could keep the selection in a combo box with the code that is below. Thanks in advance.[code=php:0]$now = time(); $lastDate = date("t", $now); $days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $thisDay = date("l"); $thisDate = date("j"); $thisMonth = date("F"); $thisYear = date("Y"); print "<select name=\"selectDay\" id=\"selectDay\">"; foreach($days as $a) { $selStr = $a == $thisDay ? " selected=\"selected\"" : ""; print "<option value=\"$a\"$selStr>$a</option>"; } print "</select>"; print "<select name=\"selectDate\" id=\"selectDate\">"; for($i=1; $i<=$lastDate; $i++) { $selStr = $i == $thisDate ? " selected=\"selected\"" : ""; print "<option value=\"$i\"$selStr>$i</option>"; } print "</select>"; print "<select name=\"selectMonth\" id=\"selectMonth\">"; foreach($months as $v) { $selStr = $v == $thisMonth ? " selected=\"selected\"" : ""; print "<option value=\"$v\"$selStr>$v</option>"; } print "</select>"; print "<select name=\"selectYear\" id=\"selectYear\">"; for($i=$thisYear; $i<$thisYear+30; $i++) { $selStr = $i == $thisYear ? " selected=\"selected\"" : ""; print "<option value=\"$i\"$selStr>$i</option>"; } print "</select>";[/code] Link to comment https://forums.phpfreaks.com/topic/35159-combo-box-question/ Share on other sites More sharing options...
linuxdream Posted January 22, 2007 Share Posted January 22, 2007 Hard to tell what you want to keep unless we know how the form is being processed/reloaded. I usually do something like this:[code]<?phpecho "<select name='something'>";foreach($options as $value=>$option){ //Set this variable depending on what is selected and how...result from DB lookup, POST to this page, etc... if(!empty($sql_result)){ $selected = "selected='selected'"; }else{ $selected = ''; } echo "<option value=$value $selected>$option</option>";}echo "</select>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/35159-combo-box-question/#findComment-166108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.