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

时间:2019-05-02 10:12:37

标签: javascript php jquery html css

我有一个带有购物车的网站,索引文件如下:

<?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>';
?>

其他2页如下:

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
}

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;
    }
}

我对PHP完全陌生。我尝试搜索很多,但没有找到我想要的。现在的问题是加载页面时,页面未显示,仍在加载中。谁能告诉我如何解决它。任何帮助都会很棒。

0 个答案:

没有答案