If else statement only showing 1st result

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:


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

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 :wink: echo it.

To check if it is:

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

If it is then do this:

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

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:


<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

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


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

should be


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

then just do a simple


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

which will count the amount that were checked upon submit

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:

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

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.

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


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