Hayato
Hayato

Reputation: 561

Yii - something wrong with if & else if statement in view file

I'm having a problem with else if statement in my view file.

I'm using Yii framework and I have the following code in my view file.

<select class="formRightPulldown" name="Post[password]">    
<?php foreach($passwordData as $password) :?>

<option value="<?php echo $password['id'] ?>" 
    <?php if(isset($updateErrorPost) && 
             $updateErrorPost['password']==$password['id']){
             echo 'selected';  
          } else if(isset($postUpdate) && 
             $postUpdate['password']==$password['id']){
             echo 'selected'; } ?>
 >
     <?php echo $password['name'] ?>
 </option>

<?php endforeach ;?>
</select>

I believe when "$updateErrorPost" is set to be true, it shouldn't read the code after "else if".

But somehow, even when "$updateErrorPost" is set to be true, it reads the code after "else if", so that "selected" value is added to multiple option tags.

Does anyone knows what's causing this problem?

Am I misunderstanding something very basic in php programming???

Please give me a help!

Thanks in advance :)

+++Added+++

It ends up adding selected value to multiple option tags like the below...

<select class="formRightPulldown" name="Post[password]">
    <option value="3">temporary</option>
    <option value="2" selected>Subscriber</option>
    <option value="1" selected>VIP</option>
</select>

In the code above, the first selected value is added when the first if-statement is true, and the second selected value is added when the else-if-statement is true.

Why although if-statement is true, code after else-if is read???

Upvotes: 1

Views: 3057

Answers (3)

Vahid Hallaji
Vahid Hallaji

Reputation: 7467

I think you want to do something like this:

foreach($passwordData as $password) {
    $selected = "";
    if((isset($updateErrorPost) && $updateErrorPost['password']==$password['id']) ||
        (isset($postUpdate) && $postUpdate['password']==$password['id'])
    ){
        $selected = "selected";
    }
    echo "<option value='{$password['id']}' {$selected}>";
    echo $password['name'];
    echo "</option>";
}

Also make sure $updateErrorPost and $postUpdate arrays has ['password'] key every time. Otherwise you should do isset() by array key. e.g. isset($updateErrorPost['password']) and another one too.

Updated: If you want to echo exactly selected word when any of 2 conditions equals true, It should be correct.

Upvotes: 0

vishal shah
vishal shah

Reputation: 222

try

<select class="formRightPulldown" name="Post[password]">    
<?php foreach($passwordData as $password) :?>

<option value="<?php echo $password['id'] ?>" 
    <?php if(isset($updateErrorPost) &&  ($updateErrorPost['password']==$password['id'])) {
             echo 'selected';  
          } else if(isset($postUpdate) && ($postUpdate['password']==$password['id'])) {
             echo 'selected';
        } 
    ?>>
     <?php echo $password['name'] ?>
 </option>

<?php endforeach ;?>
</select>

Upvotes: 1

Michał Prajsnar
Michał Prajsnar

Reputation: 672

I believe when "$updateErrorPost" is set to be true, it shouldn't read the code after "else if".

Not necessarily - since you have && $updateErrorPost['password']==$password['id'] there, then } else if {... will be run if $updateErrorPost['password'] != $password['id']

Try to make 2 seperated if { } without else having complete set of conditions and see if it works - this will help you find out where you have logic error.

Upvotes: 0

Related Questions