PDA

View Full Version : Shopping Cart Made Easy



SyntaXmasteR
09-12-2007, 05:00 PM
Here is a simple shopping cart I wrote that uses arrays in place of databases to pull products. It can be easily implemented into any small website. Hope you get a better understanding of the shopping cart functionality.



<?
/* THIS IS AN ITEM DATABASE CREATED IN ARRAY FORM.
YOU CAN BUILD A SHOPPING CART WITHOUT THE NEED
FOR A LARGE DATABASE TO STORE YOUR ITEMS */

$item[]=array(
'item_name' => 'first item',
'item_description' => 'first item description',
'item_price' => '*.**',
'item_shipping' => '0.**',
'item_image' => 'first item image'
);


$item[]=array(
'item_name' => 'second item',
'item_description' => 'second item description',
'item_price' => '2.22',
'item_shipping' => '0.22',
'item_image' => 'second item image'
);


/* THIS WILL OUTPUT EACH ELEMENT OF THE ARRAY ITEM[]
THE LOOP WILL BE BROKEN WHEN IT PRINTS THE LAST
ITEM */

foreach($item as $shopping_cart_item){
print_r(
$shopping_cart_item['item_name'] ."<br>".
$shopping_cart_item['item_description'] ."<br>".
$shopping_cart_item['item_price'] ."<br>".
$shopping_cart_item['item_shipping'] ."<br>".
$shopping_cart_item['item_image'] ."<br><br>"
);
}


/* THIS IS AN ITEM DATABASE CREATED IN ARRAY FORM BUT
INCLUDES ARRAYS IN ARRAYS THAT ALLOW EACH ITEM TO
HAVE OPTIONS SUCH AS COLOR, SIZE, ETC... */

$item[]=array( // THIS IS ITEM[2] IN THE ARRAY
'item_name' => 'third item',
'item_color' => array('red','blue','green'),
'item_size' => array('s','m','l','xl','xxl'),
'item_description' => 'third item description',
'item_price' => '*.**',
'item_shipping' => '0.**',
'item_image' => 'third item image'
);

/* THIS WILL DEMONSTRATE HOW TO ACCESS THE DIFFERENT PARTS
OF THE ARRAY IN THE ARRAY THAT YOU CREATED IN THE ABOVE
EXAMPLE. */

print_r(
$item[2]['item_color'][0] ."<br>". // THIS WILL PRINT red
$item[2]['item_size'][4] ."<br>" // THIS WILL PRINT xxp
);


/* CREATE AS MANY ITEMS AS YOU NEED FOR YOUR WEBSITE AND
SAVE THE FILE AS CATALOG.PHP - ANY OF THE ABOVE METHODS
MAY BE USED TO CREATE YOUR ITEMS. SELECT THE BEST
METHOD THAT FITS YOU!

NOW WE WILL MOVE ON TO CREATING THE ACTUAL PRODUCTS ON
THE WEBSITE. EACH PRODUCT WILL ACTUALLY BE A SEPERATE
<FORM>. DEPENDING ON WHICH PRODUCT A VISITOR SELECTS,
THAT FORM WILL BE SUBMITTED TO THE CART TELLING IT TO
ADD THAT PRODUCT TO THE CART. THIS EXAMPLE WILL SHOW
YOU EXACTLY HOW TO DO THIS. YOU CAN FORMAT THE OUTPUT
HOWEVER YOU WANT FOR APPEARANCE. THIS EXAMPLE IS USED
TO ILLUSTRATE THE FUNCTIONALITY, NOT TO LOOK PRETTY :)


BASIC PRODUCT - NO OPTIONS */
?>
<div style="margin:0; padding:0; float:left; width:205px;">
<?
foreach($item as $shopping_cart_item){ // THIS WILL GRAB EACH ITEM ONE BY ONE CREATING A FORM
?>
<div style="margin:0; padding:0; float:left; width:200px; border: 2px #000000 solid;">
<form action="cart.php" method="post">
<input type="hidden" name="action" value="add" />
<input type="hidden" name="item_name" value="<?=$item[$i]['item_name']?>" />

<div style="margin:0; padding:0; float:left; width:**5px;">
<?=$shopping_cart_item['item_name']?> - $<?=$shopping_cart_item['item_price']?>
</div>

<div style="margin:0; padding:0; float:left; width:**5px;">
<input name="item_quantity" value="0" />
</div>

<div style="margin:0; padding:0; float:left; width:**5px;">
<input type="submit" value="ADD TO CART" />
</div>

</form>
</div>
<?
}
?>
</div>



<?
/* COMPLEX PRODUCT WITH MULTIPLE COLOR & SIZE OPTIONS */
?>
<div style="margin:0; padding:0; float:left; width:205px;">
<?
foreach($item as $shopping_cart_item){ // THIS WILL GRAB EACH ITEM ONE BY ONE CREATING A FORM
?>

<div style="margin:0; padding:0; float:left; width:200px; border: 2px #000000 solid;">
<form action="cart.php" method="post">
<input type="hidden" name="action" value="add" />
<input type="hidden" name="item_name" value="<?=$item[$i]['item_name']?>" />

<div style="margin:0; padding:0; float:left; width:**5px;">
<?=$shopping_cart_item['item_name']?> - $<?=$shopping_cart_item['item_price']?>
</div>

<?
if(isset($shopping_cart_item['item_color'])){ ?>

<div style="margin:0; padding:0; float:left; width:**5px;">
<select name="item_color">

<?
$i=0;
while(isset($shopping_cart_item['item_color'][$i])){ // THIS WILL ECHO EACH COLOR OUT IN A DROP DOWN MENU
?>
<option value="<?=$shopping_cart_item['item_color'][$i]?>">
<?=$shopping_cart_item['item_color'][$i]?>
</option>
<?
$i++;
}
?>
</select>
</div>
<?
}

if(isset($shopping_cart_item['item_size'])){ ?>

<div style="margin:0; padding:0; float:left; width:**5px;">
<select name="item_size">
<?
$i=0;
while(isset($shopping_cart_item['item_size'][$i])){ // THIS WILL ECHO EACH SIZE OUT IN A DROP DOWN MENU
?>
<option value="<?=$shopping_cart_item['item_size'][$i]?>">
<?=$shopping_cart_item['item_size'][$i]?>
</option>
<?
$i++;
}
?>
</select>
</div>
<?
}
?>

<div style="margin:0; padding:0; float:left; width:**5px;">
<input name="item_quantity" value="0" />
</div>

<div style="margin:0; padding:0; float:left; width:**5px;">
<input type="submit" value="ADD TO CART" />
</div>
</form>
</div>
<?
}
?>
</div>
<?
/* FORMAT THE PRODUCTS HOWEVER YOU WOULD LIKE AND
SAVE THE FILE AS PRODUCTS.PHP. AT THE TOP OF
THIS FILE YOU MUST include("catalog.php") SO THE
PAGE WILL PRELOAD EACH ITEM INTO THE PROPPER
ARRAY.

NOW WE WILL CREATE THE ACTUAL SHOPPING CART.
EACH ITEM WILL BE STORED IN SESSION VARIABLES.
THIS IS A BASIC "ANYONE CAN DO IT" EXAMPLE. THIS
EXAMPLE DOES NOT IMPLEMENT PROPPER SECURITY
MEASURES YOU SHOULD USE TO VALIDATE SESSIONS.
YOU MAY READ MORE ABOUT SESSION SECURITY HERE
http://www.syntax******.info/scripts/secure_session_control.php */ ?>

SyntaXmasteR
09-12-2007, 05:02 PM
/* FIRST WE CREATE A SWITCH STATEMENT WITH THE POSSIBLE
$_POST['action'] VARIABLES THAT CAN BE USED. THESE
WILL INCLUDE: ADD, UPDATE, REMOVE */

session_start()
include("catalog.php");

if(isset($_POST['action'])){ // IF ACTION IS SET IT WILL ENTER THE SWITCH
switch($_POST['action']){

case "add":
add_item(); // CALLS FUNCTION TO ADD ITEM
break;

case "update":
update_item(); // CALLS FUNCTION TO UPDATE ITEM
break;

case "delete":
delete_item(); // CALLS FUNCTION TO DELETE ITEM
break;

case "empty_cart":
empty_cart(); // CALLS FUNCTION TO EMPTY CART
break;

}

}

if(isset($_SESSION['cart'])){ // DISPLAYS CART AFTER ACTION IS COMPLETED
show_cart();
}else{
echo "CART EMPTY";
}

/* NOW THAT WE KNOW WHAT YOUR CART.PHP WILL LOOK LIKE WE NEED TO
CREATE THE FUNCTIONS:
add_item()
update_item()
delete_item()
empty_cart()
show_cart()
*/

function add_item(){
/* THINGS TO THINK ABOUT BEFORE YOU ADD AN ITEM
*. DOES THE CART EXIST YET
2. DOES THIS ITEM EXIST
2. IS THIS ITEM ALREADY IN THE CART */

if(isset($_SESSION['cart'])){ // CHECKING TO SEE IF CART EXISTS
$cart=$_SESSION['cart'];
// LOOPS THROUGH EACH ITEM IN CART
$i=0;$found=0;
while(isset($cart[$i])){
// IF ITEM IS ALREADY IN CART IT WILL INCREMENT QUANTITY
if($cart[$i]['item_name']==$_POST['item_name']){
$cart[$i]['item_quantity'] += $_POST['item_quantity'];
$found=*;
}
$i++;
}
// IF ITEM IS NOT IN CART MAKE SURE ITEM EXISTS IN ITEM ARRAY
// IF IT DOES EXIST ADD IT TO CART
// IF IT DOES NOT EXIST RETURN 0 AND DO NOTHING
if($found==0){
// MAKE SURE ITEM EXISTS
$exist=0;
foreach($item as $shopping_cart_item){
if($shopping_cart_item['item_name']==$_POST['item_name']){
$exist=*;
}
}

if($exist==*){
$cart[$i]['item_name']==$_POST['item_name'];
$cart[$i]['item_quantity']==$_POST['item_quantity'];
// RETURN UPDATED CART TO SESSION
$_SESSION['cart']==$cart;
return(*);
}else{
return(0);
}
}
}
}


function update_item(){
/* THINGS TO THINK ABOUT BEFORE YOU ADD AN ITEM
*. DOES THE CART EXIST YET
2. DOES ITEM EXIST IN CART
*/
if(isset($_SESSION['cart'])){ // CHECKING TO SEE IF CART EXISTS
$cart=$_SESSION['cart'];
// LOOPS THROUGH EACH ITEM IN CART
$i=0;$found=0;
while(isset($cart[$i])){
// IF ITEM IS ALREADY IN CART IT WILL INCREMENT QUANTITY
if($cart[$i]['item_name']==$_POST['item_name']){
$cart[$i]['item_quantity'] = $_POST['item_quantity'];
$_SESSION['cart']=$cart;
return(*);
}
$i++;
}
}
return(0);
}


function delete_item(){
// THIS FUNCTION IS TRICKY BECAUSE YOU NEED TO USE THE ARRAY FUNCTIONS
/* THINGS TO THINK ABOUT BEFORE YOU ADD AN ITEM
*. DOES THE CART EXIST YET
2. DOES ITEM EXIST IN CART
*/
if(isset($_SESSION['cart'])){ // CHECKING TO SEE IF CART EXISTS
$cart=$_SESSION['cart'];
// LOOPS THROUGH EACH ITEM IN CART
$i=0;$found=0;
while(isset($cart[$i])){
// IF ITEM IS ALREADY IN CART IT WILL INCREMENT QUANTITY
if($cart[$i]['item_name']==$_POST['item_name']){
unset($cart[$i]); // POPS OFF THE ITEM IN ARRAY
$cart=array_values($cart[$i]); // REBUILDS ARRAY STACK
$_SESSION['cart']=$cart;
return(*);
}
$i++;
}
}
return(0);
}



function empty_cart(){
if(isset($_SESSION['cart'])){
unset($_SESSION['cart']);
return(*);
}
return(0);
}

function show_cart(){
$cart=$_SESSION['cart'];
$i=0;
while(isset($cart[$i])){
// THIS WILL ONLY SHOW EACH ITEM IN CART AS AN ARRAY
// YOU CAN CREATE YOUR OWN PRETTY DISPLAY. I STOP HERE.
print_r($cart[$i]) . "<br><br>";
$i++;
}
}


/* YOU NOW HAVE CREATED THREE PAGES
*. catalog.php
2. products.php
*. cart.php

CATALOG.PHP - INCLUDES ALL THE ITEMS IN YOUR STORE CREATED AS ARRAYS
PRODUCTS.PHP - HTML PAGE FOR VISITORS TO SHOP ON
CART.PHP - SHOPPING CART FOR VISITORS

WARNING: THESE EXAMPLES WERE CREATED OFF THE TOP OF MY HEAD, AND ARE UNTESTED
FOR FUNCTIONALITY. IN THEORY THEY SHOULD FUNCTION PERFECTLY. YOU SHOULD ALSO
REMEMBER THAT NO VALIDATION IS CONDUCTED IN THESE EXAMPLES. IF A VISITOR ENTERS
QUANTITY "A" IT WILL ACCEPT IT. YOU CAN MAKE YOUR OWN VALIDATION FUNCTION AS
NEEDED!
*/ ?>