0

我正在使用递增/递减来更改购物车中所需物品的数量。但是,这似乎仅适用于添加到数组中的最后一个值/行。例如,如果我在购物车中添加 1 件商品,则递增 ($_SESSION['cart'][$comic_id]++) 可以正常工作。但是,如果我将第二个项目添加到购物车,则递增仅适用于最新添加的项目,而不适用于第一个添加的项目,对于任何其他添加的项目也可以这样说。

有人可以指出我哪里出错并帮我修复我的代码吗?

我正在使用包含在 foreach 循环中的末尾的 $value 来回显。

这是我的代码(cart.php):

<?php 
if(!isset($_SESSION)) {
     session_start();
}
$total = 0;
$page = 'cart';

//Database connection info (Using MySQLi) removed for security

if(isset($_GET['comic_id'])){ 
    $comic_id = $_GET['comic_id'];
} else {
    $comic_id = NULL;}

$action = $_GET['action'];

if($comic_id && !productExists($comic_id)) {
    die("We apologise, but we can't seem to find the comic you were looking for!!!");
}

switch($action) {
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  
    case "remove":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]--;}
        if($_SESSION['cart'][$comic_id] == 0) unset($_SESSION['cart'][$comic_id]);
    break;  
    case "empty":
        unset($_SESSION['cart']); 
        session_destroy();
    break;  
}

require('header.php');
require('sidebar.php');

if (isset($_SESSION['cart'][$comic_id])){

    echo "<table border=\"0\" padding=\"10\" width=\"80%\">";
    echo "<td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";
    echo "<td colspan=\"6\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Crystal Fusion: Are you sure you wish to empty your cart?');\">Empty Cart</a></td>";                      
    echo "<tr height=\"20px\">";
    echo "<tr height=\"20px\">";
    echo "<td align=center>Image</td><td align=center>Title</td><td align=center>Description</td><td colspan=3 align=center>Copies (+/-)</td><td align=center>Price</td>";
    echo "<tr height=\"20px\">";


    foreach($_SESSION['cart'] as $comic_id => $qty) {   

        $sql = sprintf("SELECT title, description, cost, image_thumbnail
                FROM comic 
                WHERE comic_id = %d;",$comic_id);

        $result = mysql_query($sql);

        if(mysql_num_rows($result) > 0) {

            list($name, $description, $price, $image_thumbnail) = mysql_fetch_row($result);

            $cost = $price * $qty; 
            $total = $total + $cost;

            $cost = number_format($cost,2);
            $total = number_format($total,2);
            $description =  substr($description, 0, 250);

            echo "<br><tr>";
            echo "<td width=\"10px\" align=\"center\"><img height=100 align=center src=\"$image_thumbnail\">";
            echo "<td align=\"center\">$name</td>";
            echo "<td width=\"40%\" align=\"center\">$description...<a href=comic_dyn.php?comic_id=$comic_id>More Info</td>";
            echo "<td width=\"30px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=add&comic_id=$comic_id\">+<br></a><td align=\"center\">$qty <td width=\"20px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&comic_id=$comic_id\">-</a></td>";
            echo "<td align=\"right\">$$cost</td>";
            echo "</tr>";           
        }

    }

    echo "<br><tr><tr height=100px>";
    echo "<td><td><td colspan=\"4\" align=\"right\">Total:</td>";
    echo "<td width=\"60px\" align=\"right\">$$total</td>"; 
    echo "<tr><td colspan=\"7\" align=\"right\"><a href=\"checkout_html.php\">Proceed to Checkout</a>";
    echo "<tr height=\"50px\">";
    echo "</table>";
}else{

    echo "Your cart is currently empty.";
    echo "<br><br><td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";

}

$_SESSION['invoice'][$comic_id]=$name . " " . $qty . " " . $price;

foreach ($_SESSION['invoice'] as $value) {
    echo $value."<br>";}

function productExists($comic_id) {
        $sql = sprintf("SELECT * FROM comic WHERE comic_id = %d;", $comic_id);
    return mysql_num_rows(mysql_query($sql)) > 0;
    }
?>
4

1 回答 1

0

您必须在 switch 语句上执行 foreach 以从每个项目中添加/删除值。像那样

foreach($_SESSION['cart'] as $comic_id => $qty) {
switch($action) {
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  
    case "remove":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]--;}
        if($_SESSION['cart'][$comic_id] == 0) unset($_SESSION['cart'][$comic_id]);
    break;  
    case "empty":
        unset($_SESSION['cart']); 
        session_destroy();
    break;  
}


} 
于 2012-11-12T00:20:57.430 回答