Mark value as selected in drop down list created with loop

Hi

I have been trying to mark as selected, within a drop down list, a value that is returned from my DB. I have the following code that I would like to retain is possible.:

   echo "<select name='open' id='open'>\\r";

      echo "<option value=''>Select Time</option>\\r";
       
       $start = strtotime('00:00');
       $end = strtotime('23:59');

       for ($i = $start; $i <= $end; $i += 900): //30 minute increments
       
       echo "<option value='" . date('H:i', $i) . "'>" . date('H:i', $i) . "</option>\\r";   //Output as 24hr clock
                
       endfor;

      $counter++;

    echo "</select>\\r";

This produces a drop down list with 30 minute increments that I need to use on several places within my site. So far so good.

What I need it to do is to take a value from the DB and mark that value in the list as selected whilst still displaying the full list of times in the drop down to enable the user to change the value if necessary.

This is easy enough with a static drop down list but I would really like to get this working with the code I have so far. Is this possible?

Thanks

Colin

Something like this should work.

echo "<select name='open' id='open'>\\r";

      echo "<option value=''>Select Time</option>\\r";

       $start = strtotime('00:00');
       $end = strtotime('23:59');
	   //I will use variable $time as DB value
       for ($i = $start; $i <= $end; $i += 900): //30 minute increments
       $selected = (isset($time) && $time == date('H:i', $i) " selected=\\"selected\\"" : '');
       echo "<option value='" . date('H:i', $i) . "'$selected>" . date('H:i', $i) . "</option>\\r";   //Output as 24hr clock

       endfor;

      $counter++;

    echo "</select>\\r";

Thanks for the reply, glad to know it is at least possible.

it throws an error in this part though:

" selected=\\"selected\\"" : ''

unexpected T_CONSTANT_ENCAPSED_STRING

Thanks

The statement is missing the ? after the condition:

$selected = (isset($time) && $time == date('H:i', $i)) ? " selected=\\"selected\\"" : '';

Got it

      $selected = (isset($time) && $time == date('H:i', $i)) ? " selected=\\"selected\\"" : '';

Not quite right though

<option value=‘00:00’>00:00</option> <option value=‘00:15’ selected=“selected”>00:15</option>

should read

<option value=‘00:00’>00:00</option> <option selected=“selected”>00:15</option>

I kinda got to this stage before with some far messier code using if else statements and could never figure a way of not loading a value for the selected option which is why I thought you guys may come up with the answer.

I don’t understand why you’d not want a value attribute on the currently selected option?

Sorry getting late.

I have changed the code so that the correct output is: <option value=‘00:15’ selected>00:15</option>

Thanks very much for your help as ever.

Hey sorry about missing that ?

Glad you got it working.

No worries. I like to think about the replies I get here and the odd “test” does just that. I found it just as Fretburner posted.

Thanks again, all working now :slight_smile: