скрытое входное значение не отображается в цикле foreach в php - PullRequest
0 голосов
/ 26 февраля 2019

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>
...