views:

104

answers:

4

I have this code:

$thisTime = gmmktime(0, 0, 0);  
            for($i=0; $i<=95; $i++)
           {  
                $perfTimeNumber = ($i+1);  
                $perfTimestamp = $thisTime;  
                $perfTime = date("H:i", $perfTimestamp);           
        echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';   
                $thisTime = $thisTime+(15*60);
            }

This works fine to generate a select input with options from 01:00 through to 24:45 at 15 minute intervals. However, if I change the code and add an if statement I get some odd results...

$thisTime = gmmktime(0, 0, 0);

            for($i=0; $i<=95; $i++)
            {
                $perfTimeNumber = ($i+1);
                $perfTimestamp = $thisTime;
                $perfTime = date("H:i", $perfTimestamp);
                if ($perfTime == '19:30') {
                    $sel = "selected";
                }
        echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';

                $thisTime = $thisTime+(15*60);
            }

The idea is to (arbitrarily!) make the select input default to 19.30. The code above adds
selected = "selected" to every option after 19:30, not just the 19:30 option. If I change the if statement slightly to be if ($perfTime = '19:30') { ... i.e., having a single = instead of == it creates a set of options all with the value of 19:30. What am I doing wrong?

+5  A: 

Short answer: Because every single echo operation uses the current value of $sel. I assume it's initially blank, so the first N echos contain selected=''. If test succeeds, $sel is set to "selected", and every later print includes selected='selected'. If you use $perfTime = '19:30', it's an assignment, so the test always succeeds, and $sel is always 'selected'.

Quick fix: Add an else clause that sets $sel = ''. However, there are other oddities that make me think this is only a code snippit (i.e. always using $thisTime for $perfTimestamp , rather than something loop indexed, so it always prints the same time?).

Adam Wright
that makes sense. Knew it would be straight forward! Here's a weird thing though... I've changed the code to add an else clause and I can tell by looking at the source that 19:30 is now being selected. The problem is that the select menu isn't defaulting to that value!
musoNic80
Adam, what are your concerns about $thisTime? I initialise it to 1am before I start looping. Is there a better way of doing this?
musoNic80
I think that selected="" will still select that value, so you need to remove that parameter entirely for the unselected options
Tom Haigh
Just tried that and it's still not working...
musoNic80
STOP PRESS!!! Got it working! Not sure what changed, but it works! Maybe my browser was still using a cached version even though I refreshed with FnF5!
musoNic80
+4  A: 

This is because you never reset $sel.

Try this instead:

$sel = $perfTime == '19:30' ? 'selected' : '';
n3rd
A: 

$sel isn't explicitly intitialised anywhere, so it's maintaining its 'selected' value for each run through the loop.

Try $sel = ""; as the first line in your loop as a quick fix.

Matt
A: 

Hm, might be that you should do this:

...
if ($perfTime == '19:30') {
  $sel = 'selected="selected"';
}else{
  $sel = "";  
}
...

I think just having the 'selected' attribute present makes it selected.

Oops, I forgot: And

 echo '<option value="'. $perfTimeNumber .'" '.$sel.'>' .$perfTime .'</option>';
0scar
Just tried that an although it is adding it now only to 19:30, the menu isn't defaulting to 19:30. Any more ideas?
musoNic80