隐藏的输入值不显示在PHP中的foreach循环中

时间:2019-02-26 07:09:55

标签: php session-variables hidden-field

enter image description here 现在这种显示方式 我正在开发一个简单的购物车,使用名为($ _SESSION ['shopping_cart']的会话变量。我使用2个隐藏字段表示价格和商品名称。我尝试显示购物车详细信息的摘要,但显示价格和商品名称的摘要不会显示在表格中,但是会显示数量。我尝试了很多方法来解决此问题,但是它没有显示商品名称价格

如果有人能帮助我解决这个问题,我非常感谢您的指导,谢谢。

<?php 
//database connect
$connect = mysqli_connect('localhost', 'root','', 'carta');

//if session variable is set
if (isset($_POST['add_to_cart'])) {

    if (isset($_SESSION['shopping_cart']))
    {
        $item_id_array = array_column($_SESSION['shopping_cart'], 'item_id');
            if (!in_array($_GET['id'], $item_id_array)) {

                // counter to track the number of products in cart
                $count = count($_SESSION['shopping_cart']);

                //add new items to cart
                $item_array = array(
                    "item_id" => $_GET['id'],
                    "item_name" => $_POST['hidden_name'],
                    "item_price" => $_POST['hidden_price'],
                    "item_quantity" => $_POST['quantity']
                );

                $_SESSION['shopping_cart'][$count] = $item_array;

            }else{
                echo "<script>alert('Item Already Added...');</script>";
            }

    }else{
     //if shopping cart session variable doesn't exit, create array & store item details

        $item_array = array(
            "item_id" => $_GET['id'],
            "item_name" => $_POST['hidden_name'],
            "item_price" => $_POST['hidden_price'],
            "item_quantity" => $_POST['quantity']
        );

        // store item details into session variable
        $_SESSION['shopping_cart'][0] = $item_array;


    }
}
 ?>

<!DOCTYPE html>
<html>
<head>
    <title>Shopping Cart</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <!-- custom css -->
    <link rel="stylesheet" type="text/css" href="cartcustom.css">
</head>
<body>
<div class="container">
    <h3 class="text-center">Shopping Cart</h3>
    <?php 

        //get data from database
        $query = 'SELECT * FROM products ORDER BY id ASC';
        $result = mysqli_query($connect, $query);

        if ($result){
            if (mysqli_num_rows($result)>0){
                while ($row = mysqli_fetch_assoc($result)){
                    // print_r($row);

    ?>

   //form 
    <div class="col-sm-2 col-md-3">
        <form method="post" action="index.php?action=add&id=<?php echo $row['id']; ?>">

                <img src="<?php echo $row['image']; ?>" class="img-responsive"> 

                <h6 class="text-info"><?php echo $row['name']; ?></h6> 

                <h6 class="text-danger"><?php echo "Rs:" . $row['price'] . ".00" ?></h6> 

                <input type="text" name="quantity" class="form-control" value="1">

                <input type="hidden" name="hidden_name" value="<?php echo $row['name']; ?>"/>

                <input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/>

                <input type="submit" name="add_to_cart" class="btn btn-success" value="Add to Cart">

        </form>
    </div>

    <?php
                }
            }
        }

    ?>
 </div>

 //table of displaying final cart details
 <div class="container">
    <h4 class="text-center">Order Details</h5>
        <div class="table_responsive">
            <table class="table table-bordered">
                <tr>
                    <th>Item</th>
                    <th>Quantity</th>
                    <th>Price</th>
                    <th>Total</th>
                    <th>Action</th>
                </tr>

                <?php 
                    if (!empty($_SESSION['shopping_cart'])) {
                        $total = 0;
                        foreach ($_SESSION['shopping_cart'] as $keys => $values) {
                ?>

                //display the cart details to customer using a table
                <tr>
                    <td><?php echo $values["item_name"]; ?></td> // this does not display
                    <td><?php echo $values["item_quantity"]; ?></td> 
                    <td>$ <?php echo $values["item_price"]; ?></td> //this does not display
                    <td> <?php echo number_format($values['item_quantity'] * $values['item_price'], 2 ) ?></td>
                </tr>
                <?php
                        }
                    }
                 ?>
            </table>
        </div>
 </div>

</body>
</html>

0 个答案:

没有答案