Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php accessing sessions array

i am still relatively new to php and this site, so i apologise now!This is driving me crazy, i'm trying to add an array to session state for a shopping cart i am piecing together from different bits of code....

    $_SESSION['cart_items'] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);

^that is the part where it adds to the session state, this works fine as i printr and it comes out like this

Array ( [product_name] => The Ned Rose [productId] => 1 [quantity] => 1 )

This is the bit that i cant get to work. How do i access the product ID's so i can use them in a SQL query to fetch the data to populate the cart...

if(count($_SESSION['cart_items'])>0){
$ids = "";
foreach($_SESSION['cart_items']['productId'] as $id=>$value){
    $ids = $ids . $id . ",";

}

Thanks,

EDIT HERE THE CART PAGE can anyone see where i am going wrong?

<?php
session_start();
$page_title="Cart";
include 'mysql.php';
print_r($_SESSION['cart_items']);
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";

if($action=='removed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> was removed from your cart!";
echo "</div>";
}

else if($action=='quantity_updated'){
echo "<div class='alert alert-info'>";
    echo "<strong>{$name}</strong> quantity was updated!";
echo "</div>"; 
}
if(count($_SESSION['cart_items'])>0){
$ids = "";
$ids = array_keys($_SESSION['cart_items']);
foreach($_SESSION['cart_items'][$id] as $key=>$value){
    $ids = $ids . $id . ",";
}


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

//start table
echo "<table class='table table-hover table-responsive table-bordered'>";

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

    $query = "SELECT prodID, prodName, prodPrice FROM product_tbl WHERE prodID IN ({$ids}) ORDER BY prodName";
    $result = mysqli_query($db, $query);
    $total_price=0;
    while ($row = mysqli_fetch_assoc($result)){
        extract($row);

        echo "<tr>";
            echo "<td>".$row['prodName']."</td>";
            echo "<td>£".$row['prodPrice']."</td>";
            echo "<td>";
                echo "<a href='remove_from_cart.php?id=".$row['prodID']."&name=".$row['prodName']."' class='btn btn-danger'>";
                    echo "<span class='glyphicon glyphicon-remove'></span> Remove from cart";
                echo "</a>";
            echo "</td>";
        echo "</tr>";

        $total_price+=$row['prodPrice'];
    }

    echo "<tr>";
            echo "<td><b>Total</b></td>";
            echo "<td>£{$total_price}</td>";
            echo "<td>";
                echo "<a href='#' class='btn btn-success'>";
                    echo "<span class='glyphicon glyphicon-shopping-cart'></span> Checkout";
                echo "</a>";
            echo "</td>";
        echo "</tr>";

echo "</table>";

}

else{ echo ""; echo "No products found in your cart! Click Here To Return To The Store"; echo ""; }

?>

like image 230
Brent Hobson Avatar asked Jun 27 '26 03:06

Brent Hobson


1 Answers

Use $id as the KEY value when storing the product in the cart:

$_SESSION['cart_items'][$id] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);

Then you can use the ID in the foreach loop:

if( count($_SESSION['cart_items']) > 0){
    foreach($_SESSION['cart_items']['productId'] as $id => $value){
       // Get data for this product

    }
}
like image 69
Cagy79 Avatar answered Jun 29 '26 17:06

Cagy79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!