0

$_SESSION['cart'] is stored in an array, and I used foreach to get all the product id in $_SESSION['cart'] to be used in my query. however, only the top of $_SESSION['cart'] array is displayed and I can't find a way to display all products stored in the session. Every time I remove the top item from the array, the next item is displayed.

here is the code:

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

?>
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Looks like $ids has to be exploded first (on ",") and then each separate $id that was in the comma separated string $ids needs to be selected in your query – The One and Only ChemistryBlob Aug 09 '14 at 18:16
  • You might want to replace this ugly construct `$ids = ""; foreach($_SESSION['cart'] as $id){ $ids = $ids . $id . ","; } //remove the last comma $ids = rtrim($ids, ',');` with a call to [`join()`](http://php.net/manual/en/function.join.php) – Julian Aug 09 '14 at 18:18
  • Have a look at [this question](http://stackoverflow.com/questions/9736284/mysql-where-in) and the answer. – Julian Aug 09 '14 at 18:20

1 Answers1

1

You should probably change this part of code:

$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' ";

into

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

If you want to select multiple records you cannot use = operator, you should use IN (...)

You should also use mysqli function and not mysql because it's deprecated.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • i see. thanks marcin! this one does it. never have come to my mind to use implode. your rock man! thumbs up for you :) – Carl Javier Aug 09 '14 at 18:27