Search code examples
phpsessioncart

update product cart session


With some help off the internet, I was able to make a shopping cart. However, there is a problem: When I put product A in my cart, everything works fine. But when I put product A in my shopping cart again, of showing: 2x product. It shows : 1x product A, 1x product A. I tried to fix this myself, I also looked up different shopping carts how I could fix this. I found this script and try editing myself but no luck. I tried a echo to make sure if the IF function work, but no. This statement: if(in_array($productByCode[0]["productid"],$_SESSION["cart_item"])), doesn't work. This is my following code:

/*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
     }
 ?>

This code is on another page, to display the code:

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>

Solution

  • With in_array you are searching in values, and you need search in indexes of the array

    Try this:

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

    I hope it helps.

    PS. If you indent properly in your question, we are happiest :)!