# If else statement only showing 1st result

**URL:** <https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817>\
**Category:** Uncategorized\
**Created:** [June 30, 2008, 9:39pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817 "2008-06-30T21:39:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [June 30, 2008, 9:39pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/1 "2008-06-30T21:39:49Z")

</div>

Hi All

I’m kinda new to PHP and only really worked with it in flash based projects. I am having a problem and have searched everywhere, its at this stage driving me slowly insane, so I come to you all for help

Project backgroud: ok what I have is a basic content management system login, update delete add which is all working perfectly. i have a view page where the end user can select multiple checkboxes under different sections which is then forwarded to a confirm order page … depending on the checkboxes ticked and how many in each category a different response is supposed to be envoked using if else statement but I only get the first reply back no matter which boxes are checked.

here is my code:

```php

if(isset($_POST['submit'])){
$filling = $_POST['filling'];
$meat = $_POST['meat'];
    
    if ($filling <= 3){
    
echo "3 salad filling sandwich "; 
}
    
     elseif ($filling <= 2 && $meat == 1){
echo "2 salad filling 1 meat sandwich "; 
}
    elseif ($meat <= 2){
echo "2 meat sandwich "; 
}
    else 
    {
    echo " another type";
    }
}    

```

I’m completely confused as to why this wont work any help would be most appericated

Thanks in advance for any help received

Stef

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 30, 2008, 10:18pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/2 "2008-06-30T22:18:56Z")

</div>

Although I’m sure your problem is probably that ‘filling’ is not a number, this is wrong either way;

if ($filling \<= 3){

should be \>=

but then again, I don’t think it is a number 😉 echo it.

---

<div class="post-metadata">

**Author:** ![Templarian](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/templarian/32/7152_2.png) [@Templarian](https://forum.kirupa.com/u/Templarian)\
**Post date:** [June 30, 2008, 11:35pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/3 "2008-06-30T23:35:43Z")

</div>

To check if it is:

```auto
echo is_numeric($_POST['filling']);

```

If it is then do this:

```auto
$filling = intval($_POST['filling']);

```

---

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [June 30, 2008, 11:44pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/4 "2008-06-30T23:44:03Z")

</div>

ok very good point thanks a mil,

but now I’m still a bit confsed; how can I total the amount of checkboxes ticked in each section???

Here is a snippet of my code from the form:

```php

<input type="button" value="Meat" onclick="
var e = document.getElementById('meat');
if(e.style.display == 'none') e.style.display = 'block'; 
else e.style.display = 'none';
"> <p id="meat" style="display:none;">
       <?    
$sql="SELECT name FROM products WHERE type='meat'";
$result=mysql_query($sql);
         // Show records by while loop.
 while($myrow=mysql_fetch_array($result)){
 
  
?>
<input name="meat" type="checkbox" id="meat[]" value="<?php echo $myrow['name'];?> ">- <?php echo $myrow['name']; ?>
<br />
<?php
// End while loop.
}
?>

```

again any point in the general direction would be greatly appericated and thanks a mil for your help already

---

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [June 30, 2008, 11:46pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/5 "2008-06-30T23:46:00Z")

</div>

ok its def not a number Would it be another for each statement to determine the amount selected?

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [July 1, 2008, 12:52pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/6 "2008-07-01T12:52:35Z")

</div>

```php

<input name="meat" type="checkbox" id="meat[]" value="<?php echo $myrow['name'];?> ">- <?php echo $myrow['name']; ?>

```

should be

```php

<input name="meat[]" type="checkbox" id="" value="<?php echo $myrow['name'];?> ">- <?php echo $myrow['name']; ?>

```

then just do a simple

```php

<?php
// End while loop.
}
echo count($_POST['meat']);
?>

```

which will count the amount that were checked upon submit

---

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [July 1, 2008, 7:54pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/7 "2008-07-01T19:54:03Z")

</div>

thank you so much, that is at leat counting them. When i have the script finished to with the if statement working correctly ill post it

Again thanks a mil  
:thumb2:

---

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [July 1, 2008, 8:13pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/8 "2008-07-01T20:13:52Z")

</div>

i cant figure out why

elseif (($\_POST[‘filling’] \>= 2) && ($\_POST[‘meat’] == 1)){  
echo "you ordered a 2 salad filling 1 meat sandwich ";  
}

doesnt work

```php

<?php  
if(isset($_POST['submit'])){
    
    
    count($_POST['meat']);
    count($_POST['filling']);
} 

if ($_POST['filling'] >= 3){
    
echo "3 salad filling sandwich "; 
}
    
      elseif (($_POST['filling'] >= 2) && ($_POST['meat'] == 1)){
echo "you ordered a 2 salad filling 1 meat sandwich "; 
}
    elseif ($_POST['meat'] >= 2){
echo "2 meat sandwich "; 
}
    else 
    {
    echo " another type";
    }

```

thanks a mil again for any help received

---

<div class="post-metadata">

**Author:** ![GuruJi](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@GuruJi](https://forum.kirupa.com/u/GuruJi)\
**Post date:** [July 2, 2008, 7:46pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/9 "2008-07-02T19:46:59Z")

</div>

I think there’s some problem in logic. You are ‘expecting’ different results, but remember that in the case that the IF condition is successful ($\_POST[‘filling’] \>= 3), the code in that IF will be executed and the following else’s and elseif’s (associated with that IF) will be ignored … **their conditions won’t even be checked**!

In essence, the checking will only progress to the next conditional statement (within that block of IF), if the previous condition is not satisfied (false, that is). Once a condition is rendered true, it will execute the code within that condition, and then move to the next block of code (out of the IF-ELSEIF-ELSE block).

Hope this would help.

---

<div class="post-metadata">

**Author:** ![stiffi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@stiffi](https://forum.kirupa.com/u/stiffi)\
**Post date:** [July 3, 2008, 7:42pm UTC](https://forum.kirupa.com/t/if-else-statement-only-showing-1st-result/264817/10 "2008-07-03T19:42:39Z")

</div>

thanks a mil for your help im really greatful and i get what you mean but even when i set everything to = instead of \>= so i could take out that factor it is still only giving me the one result for every condition i only get the first result

```php

if ($_POST['filling'] = 3){
    echo "you ordered a 3 salad filling sandwich "; 
}
    
     elseif ($_POST['filling'] = 2 and $_POST['meat'] = 1){
echo "you ordered a 2 salad filling 1 meat sandwich "; 
}
    elseif ($_POST['meat'] = 2){
echo "you ordered a 2 meat sandwich "; 
}

```

any more suggestions im going a bit insane i have been looking at this one piece of code for way too long and i think im starting to have nightmares about it
