Page 1 of 1

Problem with if statement

Posted: Sat Sep 26, 2009 1:17 am
by wb31
I have a problem. I am built a error counter that add all the errors the user as entered. When a user enters enters numeric characters it adds the total amount of bills. My problem is I can't get the if statements nested right so the total errors or total amount of bill will show correctly. Right now both are showing at the same time. What am I doing wrong?

Code: Select all

 
<html>
<head>
  <title></title>
</head>
 
<body style="font-family: Arial, Helvetica, sans-serif; color: black;">
<h1>My Bills</h1>
<form method=post>
 
<table>
 
<tr>
<th>Item</th>
<th>Amount</th>
</tr>
 
<?php
 
    $total = "";
    
    $amount_val = $error_text;
    $error_text = $error_str;
     
    
for ($i = 1; $i < 5; $i++)
{   
    
    $counter++;
    $error_str = ""; 
    
    print "<br>i: $i";
    
    
    $item_name = 'item'.$i;
    $item_value = $_POST[$item_name];
    
    $amount = 'amount'.$i;
    $amount_value = $_POST[$amount]; 
    $amount_val = trim($amount_value);
    
    if (!empty($amount_val))
    {   
            if (is_numeric($amount_val))
            {   
                print "Add total amount<br>";
                $total = $total + $amount_val;
            } 
            if (!is_numeric($amount_val))
            { 
                print "is not numeric number<br>";
                //error_str is printing in all four feilds 
                $error_str = "<font color=red>'$counter'Amount '$amount_val' is not a number</font>"; 
                $error_cnt++; //adds 1 to $error_cnt 
                 
            }
     }    
     
    print "<tr>";
    print "<td><input type=text name=".$item_name." value='".$item_value."'></td>\n";
    print "<td><input type=text name=".$amount." value='".$amount_value."'></td>\n";
    print "<td>$error_str</td>\n";
    print "</tr>\n";  
    
}
 
?>
 
</table>
 
<?php
   //need to work on getting this to switch right!
        
if (!empty($total))
{       
        if (is_numeric($total))
        {    
            print "Add total amount<br>";
            print "Total Bills:".$total; 
        }
} else { 
        
  print "Total Bills: 0<br>";
  print "Empty<br>"; 
   
}       
        if (!is_numeric($amount_val))      
        {   
            print "Error count<br>"; 
            print "<font color=red>Errors:$error_cnt</font>";
        }
 
?>
<br><br><input type=submit value=Submit>
<br><br>
 
</form>
</body>
</html>
 

Re: Problem with if statement

Posted: Sat Sep 26, 2009 4:58 pm
by cpetercarter
At the moment your code says :

Code: Select all

if(!empty($total))  {
//print a message about the total amount
)
if(!is_numeric($amount_val)) {
//print error messages
}
 
You have two independent tests (!empty($total)) and (!is_numeric($amount_val)). It is possible to met both tests and therefore to display both the total amount and the error messages.

If you want to display the total amount only if there are no error messages, then you need something like this:

Code: Select all

if(is_numeric($amount val)) {  //ie if there are no error messages
 
      if(!empty($total)) {
      //print messages about the total
     } else {
      //say that the total is empty
    }
 
} else { //ie if there are error messages
     //print error messages
}