Jump to content

Archived

This topic is now archived and is closed to further replies.

scallywag05

if..else statement problems

Recommended Posts

Hi, Im using PHP to create a form which has the following components:
Check Buttons = $activity[]
Text fields = $duration1, $duration2...etc

The user clicks the activity and enters a duration for each. I have a series of if..else statements that when the user fills in a duration and clicks the activity check box an sql query is run. Here is an example of the code:
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--] if((!empty($duration1)) && (!empty($activity[0])))
{
$sql = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='aerobics-low impact'";
$result = mysql_query($sql, $db1);

while($row=mysql_fetch_array($result))
{
echo("<li>".$row["caloriesBurnt"]*$duration1*$weight."<br>");

}
}
else
{
print " ";
}[!--colorc--][/span][!--/colorc--]

there are 16 activities in all and when the user clicks on simultaneous boxes,ie. 1,2,3,4,5 it returns a value for each activity ok but when the user for example clicks the first box and doesnt click another box until activity 10 it only calculates activity1. Is there a way i can calculate all activites no matter what order they are selected?
thanks for the help

Share this post


Link to post
Share on other sites
Please post the code for your form. And all of your code.

Please surround your code by the "[b][/code][/b]" tag at the end of your code and "[b][code][/b]" at the beginning.

Ken

Share this post


Link to post
Share on other sites
[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]here is the code for the form:[!--colorc--][/span][!--/colorc--][code]
<form name="calories" method="post" action="../cgi-bin/cb.php">
  <p align="left"><font color="#FF0000" face="Geneva, Arial, Helvetica, sans-serif"><strong>Convert
    Kilograms <em>(kg)</em>:
    <input type="text" name="kg" ONKEYUP="document.calories.pounds.value = 2.20*document.calories.kg.value"  id="kg">
    to:
    <input name="pounds" type="text" id="pounds" disabled>
    Pounds <em>(lbs)</em></strong></font></p>
  <p align="left"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Please
    enter your weight:</strong>
    <input name="weight" type="text" id="weight" size="10">
    <em>lbs</em></font></p>
  <p align="left"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Age:</strong>
    <input name="age" type="text" id="age" size="10">
    </font></p>
  <p align="left"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Sex:</strong>
    Male
    <input name="sex" type="radio" value="Male" checked>
    Female
    <input type="radio" name="sex" value="Female">
    </font></p>
  <h3 align="left"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Activities
    <em>(per week)</em>: Week beginning: </strong></font><font face="Geneva, Arial, Helvetica, sans-serif"><strong><font color="#009933">Todays
    Date:</font>
    <INPUT type="text"  name="date" size=25 maxlength=25 value="Loading">
    </strong></font></h3>
  <p align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif"><em>Please
    select the activities and <font color="#FF0000">one duration for each</font>,
    in the table below.</em></font></strong></p>
  <table width="57%" height="277" border="1">
    <tr>
      <td width="57%"><div align="center"><strong><font face="Geneva, Arial, Helvetica, sans-serif">Activity</font></strong></div></td>
      <td width="8%"><div align="center"></div></td>
      <td width="35%"><div align="center"><strong><font face="Geneva, Arial, Helvetica, sans-serif">Duration</font></strong></div></td>
    </tr>
    <tr>
      <td>Aerobics - low impact</td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="aerobics-l" value="aerobics-low impact">
        </div></td>
      <td><input name="duration1" type="text" id="duration1" size="10">
        hours</td>
    </tr>
    <tr>
      <td>Aerobics - high impact</td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="aerobics-h" value="aerobics-high impact">
        </div></td>
      <td><input name="duration2" type="text" id="duration17" size="10">
        hours</td>
    </tr>
    <tr>
      <td>Bicycling, &lt;10mph, leisure</td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="cycling-l" value="cycling-leisure">
        </div></td>
      <td><input name="duration3" type="text" id="duration18" size="10">
        hours</td>
    </tr>
    <tr>
      <td>Bicycling, &gt;20mph, racing<br></td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="cycling-h" value="cycling-racing">
        </div></td>
      <td><input name="duration4" type="text" id="duration4" size="10">
        hours</td>
    </tr>
    <tr>
      <td>Jogging (10 minute mile, 6mph)<br></td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="jogging" value="jogging">
        </div></td>
      <td><input name="duration5" type="text" id="duration19" size="10">
        hours </td>
    </tr>
    <tr>
      <td>Golf (carrying clubs)</td>
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="golf" value="golf">
        </div></td>
      <td><input name="duration6" type="text" id="duration20" size="10">
        hours</td>
    </tr>

  </table>
  <p align="left"><font face="Geneva, Arial, Helvetica, sans-serif">
    <input type="submit" name="Submit" value="Calculate Calories Burnt">
    </font></p>

</form>
[/code]

[color=#FF0000]the code for the php the form calls is the following:
[code]
$activity = array("aerobics-low impact", "aerobics-high impact", "cycling-leisure", "cycling-racing","jogging","golf","Light housework","running-(12min mile)","running-cross country","football","squash","swim-20m/min","swim-40m/min","tai-chi","walking-slow pace","walking-brisk pace");


$activity = $_REQUEST['activity'];
$duration1 = $_REQUEST['duration1'];
$duration2 = $_REQUEST['duration2'];
$duration3 = $_REQUEST['duration3'];
$duration4 = $_REQUEST['duration4'];
$duration5 = $_REQUEST['duration5'];
$duration6 = $_REQUEST['duration6'];
$duration7 = $_REQUEST['duration7'];
$duration8 = $_REQUEST['duration8'];
$duration9 = $_REQUEST['duration9'];
$duration10 = $_REQUEST['duration10'];
$duration11 = $_REQUEST['duration11'];
$duration12 = $_REQUEST['duration12'];
$duration13 = $_REQUEST['duration13'];
$duration14 = $_REQUEST['duration14'];
$duration15 = $_REQUEST['duration15'];
$duration16 = $_REQUEST['duration16'];



if($username == '' || $weight == ''|| $age== ''|| $sex ==''||$date='') # if form incomplete
{
    print "<p>Please Complete fields marked with an (*) as these are REQUIRED</p>";
    print '<p><a href="' . $_SERVER['HTTP_REFERER'] . '">Go back to form</a></p>';

}
else
{

    // connect to the MySQL and select database
    $db1 = mysql_connect($mysqlserver, $user_id, $user_pw );
    mysql_select_db($database_name ,$db1);
    
    if($db1===false)
    {
            print "\n<br>Unable to open database";
            die(1);
    } // end if unable to open db
    

  }

?>

</p>
<form name="form1" method="post" action="">
  <table width="68%" border="1">
    <tr>
      <td width="37%"><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Activities</strong></font></div></td>
      <td width="17%"><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Duration</strong></font></div></td>
      <td width="46%"><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Calories
          Burnt</strong></font></div></td>
    </tr>
    <tr>
      <td>
        <?php
              if ( ! empty( $activity)  ) {
               foreach ( $activity as $value ) {
            print "<li>$value\n<br>";
                   }
               print " ";
               }
            
            else{
                print "You did not select an activity!!";
                }
           ?>
      </td>
      <td>
        <?php  //if statements to ensure that if a duration is filled in by
                //user it is displayed in the output table
        
            if (! empty($duration1)) {
                   print"<li>$duration1 hours\n<br>";
                  }
                else{
                   $duration1= NULL;
                }
                
            if ( ! empty($duration2)) {
                   print"<li>$duration2 hours\n<br>";
                  }
                else{
                    $duration2= NULL;
                }
                
            if ( ! empty($duration3)) {
                   print"<li>$duration3 hours\n<br>";
                  }
                else{
                        $duration3= NULL;
                }
                
            if ( ! empty($duration4)) {
                       print"<li>$duration4 hours\n<br>";
                  }
                else{
                 $duration4= NULL;
                    }
            
            if ( ! empty($duration5)) {
                   print"<li>$duration5 hours\n<br>";
                  }
                else{
                    $duration5= NULL;
                }
                
            if ( ! empty($duration6)) {
                   print"<li>$duration6 hours\n<br>";
                  }
                else{
                    $duration6= NULL;
                }
                
            if ( ! empty($duration7)) {
                   print"<li>$duration7 hours\n<br>";
                  }
                else{
                    $duration7= NULL;
                }
                
            if ( ! empty($duration8)) {
                   print"<li>$duration8 hours\n<br>";
                  }
                else{
                        $duration8= NULL;
                }
            
            
            if ( ! empty($duration9)) {
                   print"<li>$duration9 hours\n<br>";
                  }
                else{
                        $duration9= NULL;
                }
                
            if ( ! empty($duration10)) {
                   print"<li>$duration10 hours\n<br>";
                  }
                else{
                        $duration10= NULL;
                }
                
                
            if ( ! empty($duration11)) {
                   print"<li>$duration11 hours\n<br>";
                  }
                else{
                        $duration11= NULL;
                }
                
            if ( ! empty($duration12)) {
                   print"<li>$duration12 hours\n<br>";
                  }
                else{
                        $duration12= NULL;
                }
                
                        
    
            if ( ! empty($duration13)) {
                   print"<li>$duration13 hours\n<br>";
                  }
                else{
                       $duration13= NULL;
                }
            
        
            if ( ! empty($duration14)) {
                   print"<li>$duration14 hours\n<br>";
                  }
                else{
                   $duration14= NULL;
                }
            
                
            if ( ! empty($duration15)) {
                       print"<li>$duration15 hours\n<br>";                
                  }
                else{
                   $duration15= NULL;
                }
                
            
            if (! empty($duration16)) {
                   print"<li>$duration16 hours\n<br>";
                  }
                else{
                   $duration16 = NULL;
                }
        ?>
      </td>
      <td>
      <?php
      
              if((!empty($duration1)) && (!empty($activity[0])))
            {
              $sql = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='aerobics-low impact'";
            $result = mysql_query($sql, $db1);
            
            while($row=mysql_fetch_array($result)) {
                     echo("<li>".$row["caloriesBurnt"]*$duration1*$weight."<br>");    
                    }
            }
            else
            {
            print " ";
            }
        
            if((!empty($duration2)) && (!empty($activity[1])))
            {
            $sql2 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='aerobics-high impact'";
            $result2 = mysql_query($sql2, $db1);
            
            while($row=mysql_fetch_array($result2)) {
                     echo("<li>".$row["caloriesBurnt"]*$duration2*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }

            
        if((!empty($duration3)) && (!empty($activity[2])))
        {
              $sql3 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='cycling-leisure'";
            $result3 = mysql_query($sql3, $db1);
            
            while($row=mysql_fetch_array($result3)) {
                     echo("<li>".$row["caloriesBurnt"]*$duration3*$weight."<br>");
                    }    
            }
            else
            {
        print " ";
            }

        
            if((!empty($duration4)) && (!empty($activity[3])))
            {
              $sql4 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='cycling-racing'";
            $result4 = mysql_query($sql4, $db1);
            
            while($row=mysql_fetch_array($result4)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration4*$weight."<br>");                
                    }    
            }
            else
            {
            print " ";
            }
            
            
            if((!empty($duration5)) && (!empty($activity[4])))
            {
              $sql5 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='jogging'";
            $result5 = mysql_query($sql5, $db1);
            
            while($row=mysql_fetch_array($result5)) {
                     echo("<li>".$row["caloriesBurnt"]*$duration5*$weight."<br>");    
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration6)) && (!empty($activity[5])))
            {
              $sql6 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='golf'";
            $result6 = mysql_query($sql6, $db1);
            
            while($row=mysql_fetch_array($result6)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration6*$weight."<br>");
                
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration7)) && (!empty($activity[6])))
            {
              $sql7 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='Light housework'";
            $result7 = mysql_query($sql7, $db1);
            
            while($row=mysql_fetch_array($result7)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration7*$weight."<br>");
        
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration8))&&(!empty($activity[7])))
            {
              $sql8 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='running-(12min mile)'";
            $result8 = mysql_query($sql8, $db1);
            
            while($row=mysql_fetch_array($result8)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration8*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration9))&&(!empty($activity[8])))
            {
              $sql9 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='running-cross country'";
            $result9 = mysql_query($sql9, $db1);
            
            while($row=mysql_fetch_array($result9)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration9*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration10))&&(!empty($activity[9])))
            {
              $sql10 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='football'";
            $result10 = mysql_query($sql10, $db1);
            
            while($row=mysql_fetch_array($result10)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration10*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration11))&&(!empty($activity[10])))
            {
              $sql11 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='squash'";
            $result11 = mysql_query($sql11, $db1);
            
            while($row=mysql_fetch_array($result11)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration11*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration12))&&(!empty($activity[11])))
            {
              $sql12 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='swim-20m/min'";
            $result12 = mysql_query($sql12, $db1);
            
            while($row=mysql_fetch_array($result12)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration12*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration13))&&(!empty($activity[12])))
            {
              $sql13 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='swim-40m/min'";
            $result13 = mysql_query($sql13, $db1);
            
            while($row=mysql_fetch_array($result13)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration13*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            if((!empty($duration14))&&(!empty($activity[13])))
            {
              $sql14 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='tai-chi'";
            $result14 = mysql_query($sql14, $db1);
            
            while($row=mysql_fetch_array($result14)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration14*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration15))&&(!empty($activity[14])))
            {
              $sql15 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='walking-slow pace'";
            $result15 = mysql_query($sql15, $db1);
            
            while($row=mysql_fetch_array($result15)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration15*$weight."<br>");
                    }    
            }
            else
            {
            print " ";
            }
            
            if((!empty($duration16))&&(!empty($activity[15])))
            {
              $sql16 = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='walking-brisk pace'";
            $result16 = mysql_query($sql16, $db1);
            
            while($row=mysql_fetch_array($result16)) {
                     echo("<li> ".$row["caloriesBurnt"]*$duration16*$weight."<br>");
                    }    
            }
            else{
            print " ";
            }
        
       mysql_close($db1);
      ?>
      </td>
    </tr>
    <tr>
      <td colspan="2"><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"><strong>TOTAL
          CALORIES BURNT:</strong></font></div></td>
      <td><?php
      ?></td>
    </tr>
  </table>
  
</form>
</body>
[/code]

Its a bit long but it works to a point.
thanks

Share this post


Link to post
Share on other sites
You have to remember that only checkboxes that are actually checked are sent back to your script, so if only two boxes are check an array of two activities would be returned.

I suggest redoing the form to use arrays for both the activity and duration and the indices should be the activity. For example, instead of:
[code]
      <td><div align="center">
          <input name="activity[]" type="checkbox" id="aerobics-l" value="aerobics-low impact">
        </div></td>
      <td><input name="duration1" type="text" id="duration1" size="10">
        hours</td>
[/code]
do the following using the values in the $activity array in your processing script for the indices
[code]
      <td><div align="center">
          <input name="activity[aerobics-low impact]" type="checkbox" id="aerobics-l" value="aerobics-low impact">
        </div></td>
      <td><input name="duration[aerobics-low impact]" type="text" id="duration1" size="10" value="0">
        hours</td>
[/code]

Assuming you do this for the whole form, your processing script needs to change to something like
[code]<?php
if (isset($_POST['activity'])) {
    echo "You filled in the following activities and durations:<br>\n";
    foreach ($_POST['activity'] as $k => $dmy) {
            echo $k . ' for ' . $_POST['duration'][$k] . " hour(s) ...\n";
            $sql = "SELECT caloriesBurnt FROM caloriesburnt WHERE Activity='$k'";
            $result = mysql_query($sql, $db1);
            while($row=mysql_fetch_assoc($result)) {
                     echo 'Calories Burnt: ' . $row["caloriesBurnt"]*$_POST['duration'][$k]*$weight."<br>\n";    
                    }
     }
}
?>[/code]

A few comments on your original code.[list][*] In the processing script, you have form tags, but your not using a form at all.[*] Also in your processing script, I see list tags "<li>" but no "<ul>" or "<ol>" tags.[/list]
See if this works for you.

Ken

Share this post


Link to post
Share on other sites
You're welcome.

If you find yourself doing a lot of repetitive coding, I would recommend that you analyze it to see what each block of code has in common and see if the repetitions can be elminated by using a loop of some sort. Let PHP do the work, not you.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.