Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialParvez Noor
Courses Plus Student 15,917 PointsAdding multiple items to a basket page. PHP Sessions & SQL.
Hi guys,
At the moment I've managed to create a database of art images. I can then get those images from my SQL database and create html code for them to be displayed on a page (arts.php) based on category.
I've added a "purchase" button to them, which, when I click it, adds the item to the shop page (shop.php).
At the moment, I have figured out how to send one single item to the shop page using the following code:
<!-- The purchase button(on every single art item): It contains the GET queries for the item's ID and category.-->
<a href='addtocart.php?cat=".$item["category"]."&id=".$item["ID"]."' class='back-img cart-button purchase-cart' role='button'></a>
After clicking the puchase button, the following code is then executed on addtocart.php
<?php
session_start();
if(isset($_GET['id']) & !empty($_GET['id'])){
$cat = $_GET['cat'];
$items = $_GET['id'];
$_SESSION['cart'] = $items;
header('location: art.php?cat='.$cat.'&status=success');
}else{
header('location: art.php?cat='.$cat.'&status=failed');
}
?>
The user only sees the art.php refresh (although the query changes). However, in the back end, a session variable is created holding the ID of the art item clicked for purchase. This session variable is used on shop.php to create the html for the item.
The only thing is, I want to be able to add several items. I've tried to use the following code:
<?php
session_start();
if(isset($_GET['id']) & !empty($_GET['id'])){
$items = $_GET['id'];
$cat = $_GET['cat'];
$_SESSION['cart'] = $items;
header('location: art.php?cat='.$cat.'&status=success');
}else{
header('location: art.php?cat='.$cat.'&status=failed');
}
if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){
$items = $_SESSION['cart'] . "," . $_GET['id'];
$cartitems = explode(",", $items);
$_SESSION['cart'] = $items;
$_SESSION['cartItems'] = $cartitems;
header('location: art.php?cat='.$cat.'&status=success');
}else{
$items = $_GET['id'];
$_SESSION['cart'] = $items;
header('location: art.php?cat='.$cat.'&status=success');
}
?>
For some reason, though, when I click a different item, it doesn't the new ID to the list (exploding it with a comma), instead it just makes two of the most recent item clicked.
For example. Var_dump of $_SESSION['cart'] using just the single item code: string(1) "5"
var_dump using the multiple item code: string(3) "5,5"
I want it to create an array of the different items I click, so I can foreach loop cycle through the array and recreate all the items purchased to my shop.php page.
Here is the site so you can see what I mean live: https://phlegethontic-flash.000webhostapp.com/art.php
Can anyone give me any tips on what to do?
Thanks,
Parv
1 Answer
Parvez Noor
Courses Plus Student 15,917 PointsI've also tried to change the code to look like this:
<?php
if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){
$items = $_SESSION['cart'];
$items .= "," . $_GET['id'];
$cartitems = explode(",", $items);
$_SESSION['cart'] = $items;
$_SESSION['cartItems'] = $cartitems;
header('location: art.php?cat='.$cat.'&status=success');
}else{
$items = $_GET['id'];
$_SESSION['cart'] = $items;
header('location: art.php?cat='.$cat.'&status=success');
}
This presents the following var_dump, but still just logs two of the same id, and doesn't create an array bigger than 2 items big:
array(2) { [0]=> string(1) "6" [1]=> string(1) "6" }