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 */ ?>
<?
/* 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 */ ?>