Jump to content

Shopping Cart System in Php

ashraf97
Go to solution Solved by Hazy125,

how would i make like another box saying "total cost" or something under the the boxes of the amount for a giraffe?

I actually was going to add that in my actual example. But decided not to bother because you should probably be getting that information from a products database. But if you really want to add to the code I already made, you can alter the table code to be

<?php 		$total = 0;		if(!empty($_SESSION['cart'])){			foreach($_SESSION['cart'] as $product){				echo "<tr><td>$product</td><td>";				if($product == "bob"){					echo '$1500';					$total = $total + 1500;					}else if($product == "derek"){						echo '$2500';						$total = $total + 2500;						}else if($product == "harold"){							echo '$9800';							$total = $total + 9800;							}else{								echo '-';								}				echo "</td></tr>";				}			}		echo '<tr><td><strong>TOTAL</strong></td><td>$' . $total . '</td></tr>';		?>

If you aren't sure still, the live version if you follow that link again has also bee updated :)

has anyone got any guides on how i could make a online shopping cart system? link/ tutorials or have code that you have previously done

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, I lost your other thread in notifications and my mail server is currently offline so I couldn't see... Sorry about that

 

The easiest way to go about a simple shopping cart(in my opinion) is to use sessions. With sessions, unlike other php variables including things like GET/POST you can send arrays. So you can build an array that stores what items are in the cart. For example, on your products page you will have an "add to cart" button which will likely be an <a> tag of some sort. So you would have the a tag like this

<a href="cart.php?addItem=item_ID">Add to Cart</a>

Item ID is the unique product identifier. Then on cart.php do something like

<?php session_start();if(isset($_GET['addItem'])){    if(!empty($_SESSION['cart'])){        array_push($_SESSION['cart'], htmlspecialchars($_GET['addItem']));       }else{        $_SESSION['cart'] = array(htmlspecialchars($_GET['addItem']));    }}?>

This essentially grabs the item id, if there is one, and checks if the cart array exists. If it does then it will add the item to the existing array, else it will create the array. Then down where you want to display the cart items you can just loop through this array and query your database as per normal to display all the information you need about the items in the cart

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, I lost your other thread in notifications and my mail server is currently offline so I couldn't see... Sorry about that

 

The easiest way to go about a simple shopping cart(in my opinion) is to use sessions. With sessions, unlike other php variables including things like GET/POST you can send arrays. So you can build an array that stores what items are in the cart. For example, on your products page you will have an "add to cart" button which will likely be an <a> tag of some sort. So you would have the a tag like this

<a href="cart.php?addItem=item_ID">Add to Cart</a>

Item ID is the unique product identifier. Then on cart.php do something like

<?php session_start();if(isset($_GET['addItem'])){    if(!empty($_SESSION['cart'])){        array_push($_SESSION['cart'], htmlspecialchars($_GET['addItem']));       }else{        $_SESSION['cart'] = array(htmlspecialchars($_GET['addItem']));    }}?>

This essentially grabs the item id, if there is one, and checks if the cart array exists. If it does then it will add the item to the existing array, else it will create the array. Then down where you want to display the cart items you can just loop through this array and query your database as per normal to display all the information you need about the items in the cart

would the code you sent me work if i just put it in a random .php script i want to see how it works in action. i learn better visually

Link to comment
Share on other sites

Link to post
Share on other sites

would the code you sent me work if i just put it in a random .php script i want to see how it works in action. i learn better visually

 

Here we go, sorry it took so long. Decided to make it a bit pretty http://h125.co/ashraf97_cart_code

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Here we go, sorry it took so long. Decided to make it a bit pretty http://h125.co/ashraf97_cart_code

no rush dude but holy shit man this is great, so is the home page form called products.php and the other one of the cart when you press on one of the giraffe called, what is that form called or file name?

Link to comment
Share on other sites

Link to post
Share on other sites

no rush dude but holy shit man this is great, so is the home page form called products.php and the other one of the cart when you press on one of the giraffe called, what is that form called or file name?

The file names are in the URL, products.php and cart.php and the destroy.php is pretty much unnecessary for you to see so I didn't bother including it. It doesn't have anything to do with what you're asking, just makes it easier to reset the cart so you can see what's happening. But the code is

<?php session_start();session_destroy();header('Location: products.php');?>

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

 

The file names are in the URL, products.php and cart.php and the destroy.php is pretty much unnecessary for you to see so I didn't bother including it. It doesn't have anything to do with what you're asking, just makes it easier to reset the cart so you can see what's happening. But the code is

<?php session_start();session_destroy();header('Location: products.php');?>

i love you bro you've helped me so much <3 last thing is how do i take the table with all the code in it?

Link to comment
Share on other sites

Link to post
Share on other sites

i love you bro you've helped me so much <3 last thing is how do i take the table with all the code in it?

Take the table? And I assume you're talking about the one on the cart page correct?

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Take the table? And I assume you're talking about the one on the cart page correct?

yea thanks bro all done, helped out a tonne man thanks again <3

Link to comment
Share on other sites

Link to post
Share on other sites

Take the table? And I assume you're talking about the one on the cart page correct?

how would i make like another box saying "total cost" or something under the the boxes of the amount for a giraffe?

Link to comment
Share on other sites

Link to post
Share on other sites

how would i make like another box saying "total cost" or something under the the boxes of the amount for a giraffe?

I actually was going to add that in my actual example. But decided not to bother because you should probably be getting that information from a products database. But if you really want to add to the code I already made, you can alter the table code to be

<?php 		$total = 0;		if(!empty($_SESSION['cart'])){			foreach($_SESSION['cart'] as $product){				echo "<tr><td>$product</td><td>";				if($product == "bob"){					echo '$1500';					$total = $total + 1500;					}else if($product == "derek"){						echo '$2500';						$total = $total + 2500;						}else if($product == "harold"){							echo '$9800';							$total = $total + 9800;							}else{								echo '-';								}				echo "</td></tr>";				}			}		echo '<tr><td><strong>TOTAL</strong></td><td>$' . $total . '</td></tr>';		?>

If you aren't sure still, the live version if you follow that link again has also bee updated :)

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×