From: Andy McKenzie on 24 Sep 2010 14:22 I found the problem while I was copying the code over. The problem is that the if triggers in loop one, and the elseif triggers in loop two. In other words, it does exactly what it's supposed to, I just didn't think through what the loop would accomplish. Now to figure out how to make it do what I meant it to do. For the benefit of people who screwed up the same way I did: while($i < count($col_vals)) { # Start populating drop box $temp_column = $val['column']; echo '<option value="' . $col_vals[$i][$val['column']] . '"'; if($col_vals[$i][$val['column']] == $search_result[0][$col]) { echo ' selected="selected"'; } elseif($val['default'] == $col_vals[$i][$val['column']]) { echo ' selected="selected"'; } echo '>' . $col_vals[$i][$val['column']] . '</option>' . "\n"; $i++; } # End populating drop box And here's the output: <option value="vlan1201" selected="selected">vlan1201</option> <option value="none" selected="selected">none</option> <option value="vlan1288">vlan1288</option> While generating line one of the output: $col_vals[$i][$val['column']] = 'vlan1201' $search_result[0][$col] = 'vlan1201' $val['default'] = 'none' Naturally, in round one, it's selected -- that's the if statement. In round two, it's also selected -- that's the elseif statement. In round three, neither applies. To fix it, I changed the elseif to be as follows: elseif(($val['default'] == $col_vals[$i][$val['column']]) && (!isset($search_result[0][$col]))) Now the elseif only triggers if there is a default, but there is no value in the DB for that field. Hope my failure to think saves someone else some trouble later! -Alex On Fri, Sep 24, 2010 at 1:56 PM, chris h <chris404(a)gmail.com> wrote: > Andy I see no reason why both echo's would fire; unless this block of code > gets executed multiple times. can we see more of the code? > > Chris H. > > > On Fri, Sep 24, 2010 at 1:50 PM, Andy McKenzie <amckenzie4(a)gmail.com> wrote: >> >> Hey folks, >> >> Here's the deal. I have the following code: >> >> if($col_vals[$i][$val['column']] == $search_result[0][$col]) >> { echo ' selected="selected"'; } >> elseif($val['default'] == $col_vals[$i][$val['column']]) >> { echo ' selected="selected"'; } >> >> It's supposed to check whether there's a value in the db >> ($search_result[0][$col]) that matches the current column value, and >> if not, check whether the default matches it. It does that, sort of. >> In fact, both statements trigger, which I would have said wasn't >> possible. >> >> So the question is: what causes both parts of an if/elseif >> statement to trigger? As far as I can see my punctuation is correct, >> and I've confirmed through debugging statements that all the values >> are what I expect, so how do I make the elseif stop acting like >> another if? Or, alternatively, have I just misunderstood all this >> time what the if/elseif statement does? >> >> Thanks, >> Alex >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > >
From: "Jay Blanchard" on 24 Sep 2010 14:25 [snip] I am not in the majority when I say for conditions where you have more than two options use a switch control and not an elseif. In 40+ years of programming, I have never used elseif because the control confuses me. It is *much* easier for me to use, understand, and document a switch statement than an elseif. [/snip] I second this, but first I must ask; have you echo'd out the values of each item to confirm what you are evaluating? Don't get me wrong, using elseif is a fine control structure if it sees limited use, like this; if('foo' == $a){ // do foo } elseif('bar' == $a){ // do bar } else { // do glorp } If it goes beyond that I use a switch control and it has served me well for years. My bet is if you echo out the values of the items you're evaluating you will see something that will point you to the cause of your issues.
From: tedd on 24 Sep 2010 15:30 At 2:23 PM -0400 9/24/10, Bob McConnell wrote: > >A switch works when a single test can dispatch all possible branches. If >you have a series of tests where each looks for a different subset of >conditions, you need an elseif. > >Bob McConnell Bob: Not so, O'wise one. This will work: switch(1) { case $a > $b: /* whatever break; case $c == 1: /* whatever break; case $d == 'this works': /* whatever break; } Granted, it's not the normal way a switch works in some other languages, but it does work in PHP. :-) Cheers, tedd -- ------- http://sperling.com/
From: Steve Staples on 24 Sep 2010 15:44 this would be the same as: (commented below) On Fri, 2010-09-24 at 15:30 -0400, tedd wrote: > At 2:23 PM -0400 9/24/10, Bob McConnell wrote: > > > >A switch works when a single test can dispatch all possible branches. If > >you have a series of tests where each looks for a different subset of > >conditions, you need an elseif. > > > >Bob McConnell > > Bob: > > Not so, O'wise one. > > This will work: > > switch(1) > { > case $a > $b: if($a > $b) > /* whatever > break; > elseif ($c == 1) > case $c == 1: > /* whatever > break; > elseif($d == 'this works') > case $d == 'this works': > /* whatever > break; > } > Granted, it's not the normal way a switch works in some other > languages, but it does work in PHP. :-) > All you have to remember, and same as with this switch, is that the first match, will stop processing the rest of the stuff. Steve. > Cheers, > > tedd > > -- > ------- > http://sperling.com/ >
From: "Bob McConnell" on 24 Sep 2010 15:54 From: tedd > At 2:23 PM -0400 9/24/10, Bob McConnell wrote: >> >>A switch works when a single test can dispatch all possible branches. If >>you have a series of tests where each looks for a different subset of >>conditions, you need an elseif. > Not so, O'wise one. > > This will work: > > switch(1) > { > case $a > $b: > /* whatever > break; > > case $c == 1: > /* whatever > break; > > case $d == 'this works': > /* whatever > break; > } > > Granted, it's not the normal way a switch works in some other > languages, but it does work in PHP. :-) That is just so wrong, it can't actually be taken seriously. There is simply no justification for such broken logic. Bob McConnell
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Heredocs, print_r() and newline "\n" and fnmatch() -curious failures ... Next: Array question |