简体   繁体   中英

PHP multiple checkbox array with different values - Get total price of checked checkboxes

How to calculate sum of PHP multiple checkboxes values array - Total Price of checked checkboxes?

For example, result should be displayed like:

Total Price of Selected Programming Languages : C++,Java = 1200$

<form method="post" action="#">
   0<input type="checkbox" name="count[]" id="count[]" value="0"/>
   <input type="hidden" name="language[]" id="language" value="C"/>C [$800]     
   <input type="hidden" name="price[]" id="price" value="800"> <br/>

    1<input type="checkbox" name="count[]" id="count[]" value="1"/>
    <input type="hidden" name="language[]" id="language" value="C++"/>C++ [$700]
    <input type="hidden" name="price[]" id="price" value="700"> <br/>

    2<input type="checkbox" name="count[]" id="count[]" value="2"/>
    <input type="hidden" name="language[]" id="language" value="Assembler"/>Assembler [$600]
    <input type="hidden" name="price[]" id="price" value="600"><br/>

    3<input type="checkbox" name="count[]" id="count[]" value="3"/>
    <input type="hidden" name="language[]" id="language" value="Java"/>Java [$500]
    <input type="hidden" name="price[]" id="price" value="500"> <br/>

    4<input type="checkbox" name="count[]" id="count[]" value="4"/>
    <input type="hidden" name="language[]" id="language" value="PHP"/>PHP [$400]
    <input type="hidden" name="price[]" id="price" value="400"> <br/>

  <input type="submit" name="sbt" id="sbt" value="SUBMIT"> 
  </form>

This is the PHP code:

<?php
if(isset($_POST['sbt'])){

  $count = $_POST['count'];
  $sub_menu = $_POST['sub_menu'];
  $sub_price = $_POST['sub_price'];
  $sub_price1 = $_POST['sub_price'];
  //$total_price = array($sub_menu => $sub_price);

   foreach($count as $j)

    echo $sub_menu[$j] . '['.$sub_price[$j]. ']' ;
}
?>

You need to make sure the array indexes are the same for each group:

0<input type="checkbox" name="count[0]" value="0"/>
<input type="hidden" name="language[0]" value="C"/>C [$800]     
<input type="hidden" name="price[0]" value="800"> <br/>

1<input type="checkbox" name="count[1]" value="1"/>
<input type="hidden" name="language[1]" value="C++"/>C++ [$700]
<input type="hidden" name="price[1]" value="700"> <br/>

Then you can get the price and language for each count :

$price    = array_intersect_key($_POST['price'], $_POST['count']);
$language = array_intersect_key($_POST['language'], $_POST['count']);

Then implode the text and sum the price:

echo "Total Price of Selected Programming Languages: "
    . implode(',', $language) . ' = $'
    . array_sum(price);
$items = array();
$total = 0;

foreach($_POST['price'] as $k => $price) {
    if(in_array($k, $_POST['count'])) {
        $items[] = $_POST['language'][$k];
        $total += intval($price);
    }
}

$items = implode(", ", $items);
echo $items . " = $" . $total;

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