0

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?

1
  • Please use the code markdown for source code instead of HTML code tags in the future. Try out the corresponding buttons in the editor or just indent code blocks by four spaces, and surround inline code with backticks (`). Commented May 27, 2009 at 15:41

4 Answers 4

4

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?).

Sign up to request clarification or add additional context in comments.

5 Comments

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!
Adam, what are your concerns about $thisTime? I initialise it to 1am before I start looping. Is there a better way of doing this?
I think that selected="" will still select that value, so you need to remove that parameter entirely for the unselected options
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: Page cache has nothing to do with UI state. More likely you're using Firefox, which retains UI state across refreshes to be helpful.
4

This is because you never reset $sel.

Try this instead:

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

Comments

0

$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.

Comments

0

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>';

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.