在php中获取数组的每个值并在每个值上回显不同的消息

时间:2016-06-09 05:11:36

标签: php arrays

创建了许多复选框,其中用户可以选择多个复选框,而不是在发布后我将其带入数组并将其保存在会话数组中以临时存储它。我想检索会话数组变量,以便比较值并在网站上打印消息。

我正在尝试下面的代码。

HTML

<form action="cart.php" method="POST">
   <input type="checkbox" name="addon_sel[]" value="addon_1">THIS ADDON COST Rs 50 EXTRA<br>
   <input type="checkbox" name="addon_sel[]" value="addon_2">THIS ADDON COST Rs 20 EXTRA<br>
   <input type="checkbox" name="addon_sel[]" value="addon_3">THIS ADDON COST Rs 0 EXTRA<br>
   <input type="checkbox" name="addon_sel[]" value="addon_4">THIS ADDON COST Rs 10 EXTRA<br><br>
   <input type="submit" name="submit" value="Submit">

PHP(cart.php)

<?php
session_start();                           
if(isset($_POST['submit']))
{
    $oid = $_POST['oid'];     
    $addon = $_POST['addon_sel']; // ADDONS ARRAY.
    $cart = array ('addon' => $addon);
    $_SESSION['oid'] = $oid;
    $_SESSION['cart'][$oid] = $cart;
    echo "ITEMS IN YOUR CART<br>";
     foreach ($_SESSION['cart'] as $item) {             
         print_r( $item['addon'] )."<br>";     
         if ( in_array( 'addon_1',$item['addon'] ) ) 
         {
             echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 50 = Rs. 550/-" ;
         }if ( in_array( 'addon_2',$item['addon'] ) ) //<=================
         {
             echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 20 = Rs. 520/-" ;
         }if ( in_array( 'addon_3',$item['addon'] ) ) //<=================
         {
             echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 0 = Rs. 500/-" ;
         }if ( in_array( 'addon_4',$item['addon'] ) ) //<=================
         {
             echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 10 = Rs. 510/-" ;
         }
     }
}
?>

但除了使用print_r()打印的数组外,它没有显示任何内容。如果有其他方法,请帮助并告诉我。

0 个答案:

没有答案