lundi 25 mai 2015

Store multiple values in a checkbox to check with php

I have this school assignment of making an comparison site between mc and bk.

Now I have this:

<html>
<head>

<link rel="stylesheet" type="text/css" href="style1.css" />

</head>
<?php

    if(isset($_POST['formSubmit'])) 
    {
        $breakfastMC = $_POST['BreakfastMC'];
        $breakfastBK = $_POST['BreakfastBK'];
        $sumMC = 0; // <================================================================
        $sumBK = 0; // <================================================================
        if(empty($breakfastMC)) 
        {
            echo("<p>You didn't select any food in MC.</p>\n"); // <=========
        } 
        else 
        {
            $MC = count($breakfastMC);

            echo("<p>You selected for MC = $MC:<br/>"); // <=========
            for($i=0; $i < $MC; $i++)
            {   
                echo( "Price = " . $breakfastMC[$i] . "<br/>"); // <====================
                $sumMC += floatval( $breakfastMC[$i] ); // <============================
            }
            echo "Total MC = " . $sumMC . "<br/>"; // <====================================
        }
        if(empty($breakfastBK)) 
        {
            echo("<p>You didn't select any food in BK.</p>\n"); // <====================
        } 
        else 
        {
            $BK = count($breakfastBK);

            echo("<p>You selected for BK = $BK:<br/>"); // <============================
            for($i=0; $i < $BK; $i++)
            {
                echo( "Price = " . $breakfastBK[$i] . "<br/>"); // <====================
                $sumBK += floatval( $breakfastBK[$i] ); // <============================
            }
            echo "Total BK = " . $sumBK . "<br/>"; // <====================================
        }
//$sumBK = array_sum($BK)       
//$sumMC = array_sum($MC)
if ( $sumBK == $sumMC )  // <===========================================================
    echo 'equal'; // <==================================================================
else if ( $sumBK > $sumMC )  // <=======================================================
          echo 'BK more expensive than MC'; // <========================================
     else echo 'MC more expensive than BK'; // <========================================


    function IsChecked($chkname,$value)
    {
        if(!empty($_POST[$chkname]))
        {
            foreach($_POST[$chkname] as $chkval)
            {
                if($chkval == $value)
                {
                    return true;
                }
            }
        }
        return false;
    }
    }
?>


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="MC">
<h2><b>Mc Donalds</b></h2>
<br />
<b>Ontbijt:</b>
<br />

<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]" id="squaredOne1"   value="2"/>
      <label for="squaredOne1"></label>
      <div class="McText">Croissant jam en boter Prijs = 2      Cal = 100</div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne2"   value="3"/>
      <label for="squaredOne2"></label>
      <div class="McText">Pancakes met stroop <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne3"   value="3"/>
      <label for="squaredOne3"></label>
      <div class="McText">McMuffin met Bacon en Ei <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne4"   value="4"/>
      <label for="squaredOne4"></label>
      <div class="McText">McMuffin met Kaas <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne5"   value="2"/>
      <label for="squaredOne5"></label>
      <div class="McText">2x Versgemalen koffie <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne6"   value="2"/>
      <label for="squaredOne6"></label>
      <div class="McText">2x Thee <br /></div>
</div>
<b>Lunch:</b>
<br />
<div class="squaredOne"><input type="checkbox" name="LunchMC[]" id="squaredOne7"   value="2"/>
      <label for="squaredOne7"></label>
      <div class="McText">Hamburger <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="LunchMC[]" id="squaredOne8"   value="3"/>
      <label for="squaredOne8"></label>
      <div class="McText">Cheeseburger <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="LunchMC[]" id="squaredOne9"   value="4"/>
      <label for="squaredOne9"></label>
      <div class="McText">1955 met bacon <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="LunchMC[]" id="squaredOne10"   value="3"/>
      <label for="squaredOne10"></label>
      <div class="McText">McKroket <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne11"   value="2"/>
      <label for="squaredOne11"></label>
      <div class="McText">Coca Cola Middel (0,4L) <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne12"   value="4"/>
      <label for="squaredOne12"></label>
      <div class="McText">Coca Cola Groot (0,5L) <br /></div>
</div>

<b>Avondeten:</b>
<br />
<div class="squaredOne"><input type="checkbox" name="DinerMC[]" id="squaredOne13"   value="4"/>
      <label for="squaredOne13"></label>
      <div class="McText">Big Mac <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="DinerMC[]" id="squaredOne14"   value="3"/>
      <label for="squaredOne14"></label>
      <div class="McText">Chicken Sensation <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="DinerMC[]" id="squaredOne15"   value="5"/>
      <label for="squaredOne15"></label>
      <div class="McText">Chili Chicken <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="DinerMC[]" id="squaredOne16"   value="4"/>
      <label for="squaredOne16"></label>
      <div class="McText">Big Tasty met bacon <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne17"   value="5"/>
      <label for="squaredOne17"></label>
      <div class="McText">Coca Cola Mega (0,7L) <br /></div>
</div>
<div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne18"   value="4"/>
      <label for="squaredOne18"></label>
      <div class="McText">Coca Cola Groot (0,5L) <br /></div>
</div>

<br />

</div>

<div class="BK">

<h2><b>Burger King </b></h2>

<b>Ontbijt:</b>
<br />
<input type="checkbox" name="BreakfastBK[]"   value="1.50">  Mini Pancakes (9stuks) €1,50 364kcal<br />
<input type="checkbox" name="BreakfastBK[]"   value="1.50">  Toastie €1,50 359 kcal<br />
<input type="checkbox" name="BreakfastBK[]"   value="3.00">  Breakfast Burger €3,00  825 kcal <br />
<input type="checkbox" name="BreakfastBK[]"   value="2.75">  Breakfast Wrap €2,75 441 kcal<br /> <br />
<input type="checkbox" name="BreakfastBK[]"   value="3.80">  2xKoffie €3,80 0 kcal<br />
<input type="checkbox" name="BreakfastBK[]"   value="2.25">  1 flesje Chaudfontaine (500 ml) €2,25 0 kcal<br />

<b>Lunch:</b>
<br />
<input type="checkbox" name="LunchBK[]"   value="4.55">  Grilled Chicken Wrap €4,55  344 kcal<br />
<input type="checkbox" name="LunchBK[]"   value="1.95">  Cheeseburger €1,95 338 kcal<br />
<input type="checkbox" name="LunchBK[]"   value="5.75">  Tripple Whopper €5,75  1114 kcal<br />
<input type="checkbox" name="LunchBK[]"   value="4.75">  Big King XXL  €4,75 1022 kcal<br /> <br />
<input type="checkbox" name="BreakfastBK[]"   value="2.00">  Coca Cola M (400 ml)  €2,00 168 kcal<br />
<input type="checkbox" name="BreakfastBK[]"   value="2.50">  Coca Cola XXL (750 ml) €2,50  315 kcal<br />

<b>Avondeten:</b>
<br />
<input type="checkbox" name="DinerBK[]"   value="4.75">  Double Cheeseburger XXL  €4,75  502 kcal <br />
<input type="checkbox" name="DinerBK[]"   value="5.25">  Grilled Chicken BBQ €5,25 707 kcal<br />
<input type="checkbox" name="DinerBK[]"   value="3.25">  Whopper Jr. €3,25 362 kcal  <br />
<input type="checkbox" name="DinerBK[]"   value="3.75">  Big King €3,75 552 kcal <br /> <br />
<input type="checkbox" name="BreakfastBK[]"   value="2.25">  Coca Cola M (400 ml) €2,25 210 kcal <br />
<input type="checkbox" name="BreakfastBK[]"   value="2.50">  Coca Cola XXL (750 ml) €2,50  315 kcal<br />

</div>
<div class="Button">
<input type="submit" name="formSubmit" value="submit">
</div>


</form>

</body>
</html>

This code compares the values from MC and BK but only the price. Is it possible to somehow add like a value2 to a checkbox for the calories, so they get compared as well?

Something like this:

<input type="checkbox" name="BreakfastMC[]" value1="10" value2="20"/>

Or any other way?

this is the site btw: http://ift.tt/1JReCcA [warning: possibly compromised]




Aucun commentaire:

Enregistrer un commentaire