简体   繁体   中英

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://informaticaphp.net84.net/home.php [warning: possibly compromised]

Don't put the price in the value, just put an identifier for the product there, eg

 <div class="squaredOne"><input type="checkbox" name="BreakfastMC[]" id="squaredOne1"   value="croissant"/>
 <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="pancakes"/>
 <label for="squaredOne2"></label>
 <div class="McText">Pancakes met stroop <br /></div>
 </div>
 <div class="squaredOne"><input type="checkbox" name="BreakfastMC[]"  id="squaredOne3"   value="mcmuffinbacon"/>
 <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="mcmuffinkaas"/>
 <label for="squaredOne4"></label>
 <div class="McText">McMuffin met Kaas <br /></div>
 </div>

Use an array to map the product IDs to the price and calories:

$MCmenu = array('croissant' => array('price' => 2, 'calories' => 100),
                'pancakes' => array('price' => 3, 'calories' => 200),
                'mcmuffinbacon' => array('price' => 3, 'calories' => 450),
                'mcmuffinkaas' => array('price' => 4, 'calories' => 350),
                ...);
$BKmenu = array('pancakes' => array('price' => 1.5, 'calories' => 364),
                'toastie' => array('price' => 1.5, 'calories' => 359),
                'breakfastburger' => array('price' => 3, 'calories' => 825),
                ...);

Then when you're calculating the price, you can do:

 foreach($breakfastMC AS $bk)
     {
         echo( "Price = " . $MCmenu[$bk]['price'] . "<br/>");
         echo( "Calories = " . $MCmenu[$bk]['calories'] . "<br/>");
         $sumMC += $MCmenu[$bk]['price'];
         $caloriesMC += $MCmenu[$bk]['calories'];

     }

To further improve this, you can put descriptions in the menu arrays

$MCmenu = array('croissant' => array('price' => 2, 'calories' => 100, 'desc' => 'Croissant jam en boter Prijs'),
                'pancakes' => array('price' => 3, 'calories' => 200, desc => 'Pancakes met stroop'),
                'mcmuffinbacon' => array('price' => 3, 'calories' => 450, 'desc' => 'McMuffin met Bacon en Ei')
                ...);

Then you can generate the HTML from this:

foreach ($MCmenu as $id => $item) {
    $price = $item['price'];
    $cal = $item['calories'];
    $desc = $item['desc'];
    ?>
 <div class="squaredOne"><input type="checkbox" name="BreakfastMC[]" id="BreakfastMC<?php echo $id ?>"   value="<?php echo $id ?>"/>
 <label for="BreakfastMC<?php echo $id ?>"></label>
 <div class="McText"><?php echo $desc ?> = <?php echo $price ?>       Cal = <?php echo $cal ?></div>
 </div>
<?php }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM