My First Shopping Cart

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

My First Shopping Cart

Post by <br> »

Here's my first stab at building a shopping cart. See anything that might cause me trouble later? Right now all it does is pass # of items and order total back to index.php so that I can display the shopping cart summary. Next I'll tackle the full shopping cart page, change QTY, delete, etc.

Code: Select all

$cart = $_SESSION['cart'];
if ($cart) {
    $cart .= ','.$_GET['id'];
} else {
    $cart = $_GET['id'];
}
$_SESSION['cart'] = $cart;
$column==1;
$items = explode(',',$cart,100);
$inlist = implode(", ",$items);
$contents = array();
foreach ($items as $item) {
    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$con = mysql_connect($db, $user, $pass) or die(mysql_error());
mysql_select_db($db2) or die(mysql_error());
$query  = "SELECT $select FROM $table WHERE id IN ($inlist)";
$result = mysql_query($query);
$column=1;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $p=$row[id];
    $q=$contents[$p];
    $c=$row[price];
    $_SESSION[$column] =($c*$q);
    $column++;
}
$ppp=$column-1;
$ttt=1;
$additives = array();
while ( $ttt <= $ppp ){
    $additives[$ttt] = $_SESSION[$ttt];
    $ttt++;
}
$_SESSION['total']=array_sum($additives);
Also, my pages are pull based on an exploded URL... for example, the "sign in" page code is displayed when

Code: Select all

$exploded_URL[1]=='signin'
Categories are pulled because my table has a column that mimics my url directories... so if the url is '/Earrings/Gold', it pulls everything that has 'Earrings - Gold' in it's 'category column' (aka '$exploded_url[1] + " - " + $exploded_url[2]'). Is this a common practice or am I setting my self up for disaster?

Thanks for your input
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: My First Shopping Cart

Post by Christopher »

I think the first improvement is that you can store arrays in the session, so I would recommend doing that instead of concat/explode. You should also do some better filtering and validation of the Request values.
(#10850)
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: My First Shopping Cart

Post by pkbruker »

Actually, I'd try a different approach. Make a sepparate class for storing the cart data. You can use serialize() and unserialize() to store/retrieve the data in a database.

Using a class gives a whole lot more structure to how the cart works. Here is a small example of functions you might wanna include:

Code: Select all

 
class Cart {
 
  public function __construct() {
  }
  
  // Insert an item in the cart
  public function insert() {
  }
  
  // Remove an item from the cart
  public function remove() {
  }
  
  // Calculate total shipping cost based on cart contents
  public function calculateShipping() {
  }
  
  // Calculate total cost of items in cart
  public function calculateItems() {
  }
  
  // Calculate total cost of purchase
  public function calculateTotal() {
  }
  
  // Get all items in cart as an array
  public function getItems() {
  }
 
}
 
Post Reply