du127953 2019-05-02 10:12
浏览 53

PHP - 购物车未在页面上加载

I have a website with a cart, the index file is as below:

<?php
session_start();

include_once 'product-action.php';
include_once 'cart.php';

$stmt = $conn->prepare("SELECT * FROM entertainment ORDER BY id ASC");
$stmt->execute();
$entertainment = $stmt->get_result();
echo '<table style="border: 1px solid #ddd;"><tr>';
if (!empty($entertainment)) {
    foreach($entertainment as $product){
?>
    <td style="border: 1px solid #ddd;">
    <form method="post" action=""Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location?action=add&id=<?php echo $product['id']; ?>">
    <div class="product-image"><img src="<?php echo $product['image']; ?>" width="200" height="200"></div>
    <div><strong><?php echo $product["title"]; ?></strong></div>
    <div class="product-price"><?php echo "$".$product["offer_price"]; ?></div>
    <div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" /></div>
    </form>
    </td>
<?php 
    }
}
echo '</tr></table>';
?>

The other 2 pages are as follows:

cart.php

<?php
if(!empty($_SESSION["cart_item"])){
    $item_total = 0;
?>
    <a href="index.php?action=empty"><input type="button" value="Empty Cart"></a>
    <table cellpadding="10" cellspacing="1">
    <tbody>
    <tr>
    <th><strong>Name</strong></th>
    <th><strong>Quantity</strong></th>
    <th><strong>Price</strong></th>
    <th><strong>Action</strong></th>
    </tr>
<?php
    foreach ($_SESSION["cart_item"] as $item){
?>
        <tr>
        <td><strong><?php echo $item["product_title"]; ?></strong></td>
        <td><?php echo $item["quantity"]; ?></td>
        <td><?php echo "$".$item["price"]; ?></td>
        <td><a href="index.php?action=remove&id=<?php echo $item["id"]; ?>" class="btnRemoveAction">Remove Item</a></td>
        </tr>
<?php
        $item_total += ($item["price"]*$item["quantity"]);
    }
?>

<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
<?php
if(!empty($_SESSION["cart_item"])){
    $item_total = 0;
?>
    <a href="index.php?action=empty"><input type="button" value="Empty Cart"></a>
    <table cellpadding="10" cellspacing="1">
    <tbody>
    <tr>
    <th><strong>Name</strong></th>
    <th><strong>Quantity</strong></th>
    <th><strong>Price</strong></th>
    <th><strong>Action</strong></th>
    </tr>
<?php
    foreach ($_SESSION["cart_item"] as $item){
?>
        <tr>
        <td><strong><?php echo $item["title"]; ?></strong></td>
        <td><?php echo $item["quantity"]; ?></td>
        <td><?php echo "$".$item["offer_price"]; ?></td>
        <td><a href=""Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location?action=remove&id=<?php echo $item["id"]; ?>" class="btnRemoveAction">Remove Item</a></td>
        </tr>
<?php
        $item_total += ($item["offer_price"]*$item["quantity"]);
    }
?>

<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}

The product_action.php

<?php
if(!empty($_GET["action"])) {
    $productId = isset($_GET['id']) ? htmlspecialchars($_GET['id']) : '';
    $quantity = isset($_POST['quantity']) ? htmlspecialchars($_POST['quantity']) : '';
    switch($_GET["action"]) {
    case "add":
        if(!empty($quantity)) {
            $stmt = $conn->prepare("SELECT * FROM entertainment where id= ?");
            $stmt->bind_param('i',$productId);
            $stmt->execute();
            $productDetails = $stmt->get_result()->fetch_object();
            $itemArray = array($productDetails->id=>array('product_title'=>$productDetails->title, 'id'=>$productDetails->id, 'quantity'=>$quantity, 'price'=>$productDetails->offer_price));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productDetails->id,array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                        if($productDetails->id == $k) {
                            if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                $_SESSION["cart_item"][$k]["quantity"] = 0;
                            }
                            $_SESSION["cart_item"][$k]["quantity"] += $quantity;
                        }
                    }
                } else {
                    $_SESSION["cart_item"] = $_SESSION["cart_item"] + $itemArray;
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
        break;
    case "remove":
        if(!empty($_SESSION["cart_item"])) {
            foreach($_SESSION["cart_item"] as $k => $v) {
                if($productId == $v['id'])
                    unset($_SESSION["cart_item"][$k]);
                }
            }
        break;
    case "empty":
        unset($_SESSION["cart_item"]);
    break;
    }
}

I am completely new to PHP. I tried searching alot but didn't find what I wanted. now the problem is while loading the page, the page is not displaying, its still loading. can anyone tell me how to fix it. any help would be great.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 yolov8边框坐标
    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真