php - 显示 session 数组中的所有项目。仅显示顶部项目

标签 php mysql arrays session session-variables

$_SESSION['cart']存储在数组中,我使用了 foreach获取 $_SESSION['cart'] 中的所有产品 id在我的查询中使用。然而,只有 $_SESSION['cart'] 的顶部显示数组,但我找不到显示 session 中存储的所有产品的方法。每次我从数组中删除顶部项目时,都会显示下一个项目。

这是代码:

<?php
$action = isset($_GET['action']) ? $_GET['action'] : "";

if ($action == 'removed') {
    echo "<div>" . $_GET['name'] . " was removed from cart.</div>";
}

if (isset($_SESSION['cart'])) {
    $ids = "";
    foreach ($_SESSION['cart'] as $id) {
        $ids = $ids . $id . ",";
    }

    //remove the last comma
    $ids = rtrim($ids, ',');

    $query = "SELECT productID, productName, productPrice FROM tblproducts WHERE productID='$ids' ";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);

    if ($num_rows > 0) {
        echo "<table border='0'>"; //start table

        // our table heading
        echo "<tr>";
        echo "<th class='textAlignLeft'>Product Name</th>";
        echo "<th>Price (PHP)</th>";
        echo "<th>Action</th>";
        echo "</tr>";

        //also compute for total price
        $totalPrice = 0;

        while ($row = mysql_fetch_array($result)) {

            $totalPrice += $row['productPrice'];

            //creating new table row per record
            echo "<tr>";
            echo "<td>" . $row['productName'] . "</td>";
            echo "<td class='textAlignRight'>" . $row['productPrice'] . "</td>";
            echo "<td class='textAlignCenter'>";
            echo "<a href='remove-from-cart.php?id=" . $row['productID'] . "&name=" . $row['productName']
                . "'\" class='custom-button'>";
            echo "<img src='../../images/front/removefromcart.png' title='Remove from cart' width='80' height='50' />";
            echo "</a>";
            echo "</td>";
            echo "</tr>";
        }

        echo "<tr>";
        echo "<th class='textAlignCenter'>Total Price</th>";
        echo "<th class='textAlignRight'>{$totalPrice}</th>";
        echo "<th></th>";
        echo "</tr>";

        echo "</table>";
        echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
    } else {
        echo "<div>No products found in your cart. :(</div>";
    }
} else {
    echo "<div>No products in cart yet.</div>";
}

?>

最佳答案

您可能应该更改这部分代码:

$ids = "";
foreach($_SESSION['cart'] as $id){
    $ids = $ids . $id . ",";
}

//remove the last comma
$ids = rtrim($ids, ',');

$query = "SELECT productID, productName, productPrice FROM tblproducts WHERE productID='$ids' ";

进入

$query = "SELECT productID, productName, productPrice FROM tblproducts WHERE productID IN (".implode(',', $_SESSION['cart'])." )";

如果要选择多条记录,则不能使用 = 运算符,应使用 IN (...)

您还应该使用 mysqli 函数,而不是 mysql,因为它已被弃用。

关于php - 显示 session 数组中的所有项目。仅显示顶部项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221611/

相关文章:

php - 使用 PHP 删除编码

php - 从远程服务器从phpstorm中的键盘快捷方式运行脚本

c# - 如何用C#命令控制服务中存在Mysql Server?

mysql - 如何将我的结果命名为其他名称

c++ - 在 C++ 中初始化结构中的 const 数组

javascript - Node.js 中的 Array.forEach 是异步的吗?

php - 用于创建管理面板的 Yii 扩展

mysql - SQL是否可以组合group、count和distinct?

javascript - 如何将子数组对象插入javascript中的数组

javascript - 数据库条目中的 html 标签未呈现为 html