繁体   English   中英

更新产品购物车会话

[英]update product cart session

在互联网帮助下,我得以购物。 但是,有一个问题:将产品A放入购物车后,一切正常。 但是,当我再次将产品A放入购物车时,显示:2倍产品。 它显示:1x产品A,1x产品A。我自己尝试修复此问题,我还查看了不同的购物车如何解决此问题。 我找到了这个脚本并尝试编辑自己,但没有运气。 我试过回声以确保IF函数是否起作用,但是没有。 该语句: if(in_array($productByCode[0]["productid"],$_SESSION["cart_item"]))无效。 这是我的以下代码:

/*add product */
if(!empty($_POST["quantity"])) {
    $con = new DBController();
    $productByCode =  $con->runQuery ("SELECT * FROM opdracht14_product WHERE productid='" . $_GET["productid"] . "'");
    $itemArray = array($productByCode[0]["productid"]=>array('naam'=>$productByCode[0]["naam"], 'productid'=>$productByCode[0]["productid"], 'quantity'=>$_POST["quantity"], 'prijs'=>$productByCode[0]["prijs"]));

    if(!empty($_SESSION["cart_item"])) {
        if(in_array($productByCode[0]["productid"],$_SESSION["cart_item"])) {
            foreach($_SESSION["cart_item"] as $k => $v) {
                if($productByCode[0]["productid"] == $k)
                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                    echo "hoia";
            }
        } else {
            $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            echo "hoib";
        }
    } else {
        $_SESSION["cart_item"] = $itemArray;
        echo "hoic";
    }
}

<?php
while($row = mysqli_fetch_array($result)) {
?>
    <form method="post" action="opdracht14.php?action=add&productid=<?php echo $row["productid"]; ?>">
        <h3><?php echo $row['naam'] ?></h3>
        <br/>
        <img alt="<?php echo $row['alt'] ?>" src="afbeeldingen/<?php echo $row['link']?>"><br/>
        Prijs:<br/>
        <span name="naam"><?php echo $row['prijs'] ?></span><br/>
        Omschrijving:<br/>
        <p><?php echo $row['omschrijving'] ?> </p>
        <br/>
        <input class="hoeveelheid" type="number" name="quantity" value="1">
        <input  type="submit" value="bestellen" name="submit">
    </form>
 <?php
     }
 ?>

此代码在另一页上,以显示代码:

if(isset($_SESSION["cart_item"])){
    $item_total = 0;
?>  
<table cellpadding="10" cellspacing="1">
    <tbody>
        <tr>
            <th><strong>Name</strong></th>
            <th><strong>Productcode</strong></th>
            <th><strong>Quantity</strong></th>
            <th><strong>Price</strong></th>
        </tr>   
<?php       
    foreach ($_SESSION["cart_item"] as $item){
?>
        <tr>
            <td><?php echo $item["productid"]; ?></td>
            <td><?php echo $item["naam"]; ?></td>
            <td><?php echo $item["quantity"]; ?></td>
            <td align=right><?php echo "€ ".$item["prijs"]; ?></td>
        </tr>
  <?php
      $item_total += ($item["prijs"]*$item["quantity"]);
    }
}
    ?>

    </table>
</div>

使用in_array您正在搜索值,并且需要在数组的索引中搜索

尝试这个:

if(isset($_SESSION["cart_item"][$productByCode[0]["productid"]]))

希望对您有所帮助。

PS。 如果您在问题中正确缩进,我们将是最幸福的:)!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM