Shopping cart using sessions---suggestion needed please.

A

Anonymous

Guest
Anyone knows where i could find an example of a PHP shopping cart using sessions instead of having queries whenever a request(add, edit, delete) on cart is made.

I'm making one but i'm currently having problems.

here's my code. I wish to be able to list the order in a table and show quantity. I plan to put a textfield wherein they could change the quantity and update. upon update $price_ar should be able to change.

Suggestions pls......
Code:
<? 
session_start();
require("cn.php");
session_register("item_id");
session_register("order_qty");
$order_qty = $_POST['order_qty'];
session_register('order_qty');
$item_id = $_POST['item_id'];
$select = "select * from item where item_id like '".$item_id."%' order by item_id ASC";
$rs = mysql_query($select) or die(mysql_error());
$count = mysql_num_rows($rs);
echo $count." results found!<br><br>";
if($count==1){
	$rows = mysql_fetch_array($rs); //kunin as array results
	$item_id = $rows["item_id"]; 
	$description = $rows["description"];
	$s_price = $rows["price"];
	$price = $order_qty * $rows["price"];
	$qty = $rows["qty"];
	if(!isset($_SESSION["order"])){
	$order = array( 'item:' => $item_id, 'description:' => $description, 'price:' => $price);
	session_register('order');
	}else{
	array_push($order,$item_id,$description,$price);
	}
	if(!isset($_SESSION["item"])){
	$item = array($item_id);//lagay sa array values
	$desc = array($description);
	$price_ar= array($price); 
	print_r($test);
	session_register('item');
	session_register('desc');
	session_register('price_ar');
	} else { 
	array_push($item, $item_id); :help:  :help: 
	array_push($desc, $description);
	array_push($price_ar, $price);
	session_register('item');
	session_register('desc');
	session_register('price_ar');
	}
	}
?>
	<table border="1">
	<tr>
	<? while (list($key,$value) = each($order)) {
		echo "<td> $key : $value </td></tr><tr>";
		}?>
	</tr>
	</table>
 
First of all try assigning values directly to a session variables, like: $_SESSION['order_qty'] = 'whatever'; instead of register it first with session_register();.
 
the order_qty comes from the form before this page.

Code:
		<table id="inside">
                                <form action="testing.php" method="post">
		<tr>
		<td><input name="item_id" type="hidden" value="1"></td>
		</tr>
		<tr>
		<td>Quantity: <input name="order_qty" type="text" size="3" maxlength="3"></td>
		</tr>
		<tr>
		<td><input name="Submit" type="submit" value="Add to Cart"></td>
		</tr>
		</form>
		</table>

any suggestions?
 
That's right... superglobals like you do.

About session_register(), you don't need it sinse it is deprecated.
Just use the above method when you need to assign a value to some session variable.

Now tell me. Are you losting sessions values or is your problem in the SQL part !?
 
problem as ff:

1. can't combine session arrays into one.
2. if user picks the same item quantity should add and description should not double in my desc session array.
3. no update quantity feature as seen in almost all of the shopping carts.
 
revised my code: followed and modified example from phpbuilder.

planning to have a remove from basket option using checkbox. ---please see show basket part.
problem now is how to remove $name $price $amount $price2 from their respective arrays.
any suggestions on how to do it. I'm not good in handling session arrays.


Code:
<?php
if ($basket!=""){
   	if (session_is_registered("ses_basket_items")){ // check if registered
		$basket_position_counter=0; //if basket has item.
		$double=0;
		if ($ses_basket_items>0){
		   foreach ($ses_basket_name as $basket_item){
		      if ($basket_item==$basket){ //check for same entry.
		         $double=1;
		         $basket_position=$basket_position_counter;
		      }
		      $basket_position_counter++;
		   }
		}
		if ($double==1){ //update price for the same entry.
		   $oldamount=$ses_basket_amount[$basket_position];
		   $ses_basket_amount[$basket_position]++;
		   $amount=$ses_basket_amount[$basket_position];
		   $oldprice=$ses_basket_price[$basket_position];
		   $newprice=($oldprice/$oldamount)*$amount;
		   $ses_basket_price[$basket_position]=$newprice;
		   $total=$total+($oldprice/$oldamount);
		   /*foreach($ses_basket_price as $v){
	 	   $total = $total + $v;
		   }*/
		}else{
		   $ses_basket_name[]=$basket;
		   $ses_basket_amount[]=1;
		   $ses_basket_price[]=$price;
		   $ses_basket_price2[]=$price2;
		   $ses_basket_id[]=$id;
		   $ses_basket_items++;
		   $total=$total+$price;
		}
	}else{
		$ses_basket_items=1; //start basket, would not pass this block if $sesbasket_items is registered.
		$ses_basket_name[0]=$basket;
		$ses_basket_amount[0]=1;
		$ses_basket_price[0]=$price;
		$ses_basket_price2[0]=$price2;
		$ses_basket_id[0]=$id;
		$total=$price;
		session_register("total");
		session_register("ses_basket_items");
		session_register("ses_basket_name");
		session_register("ses_basket_amount");
		session_register("ses_basket_price");
		session_register("ses_basket_price2");
		session_register("ses_basket_id");
	}
}
echo "<table border=1>";// show basket
echo "<tr><td> Qty </td><td> Item </td><td> item_price </td><td> price </td></tr>";
if ($ses_basket_items>0){
   for ($basket_counter=0;$basket_counter<$ses_basket_items;$basket_counter++){
   	  $price2=sprintf("%01.2f",$ses_basket_price2[$basket_counter]);
      $price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
      $amount=$ses_basket_amount[$basket_counter];
      $name=$ses_basket_name[$basket_counter];
	  $total_price=$total;
	  
	  echo "<tr><td>".$amount."</td><td>".$name."</td><td>".$price2."</td><td bgcolor=#eeeeee>".$price."</td></tr>";
      
   }
} else {
	$ses_basket_items=0;
	unset($ses_basket_name);
	unset($ses_basket_amount);
	unset($ses_basket_price);
	unset($ses_basket_id);
}
echo "<tr><td colspan=3> total price is: </td><td bordercolor=#ff0000>".$total."</td></tr>";
echo "</table>";
?>
 
Just make some condition for the deletion part and then use the next code:
Code:
<?php

// To delete/destroy session variables
unset($_SESSION['ses_basket_name'], $_SESSION['ses_basket_amount'], 
      $_SESSION['ses_basket_price'], $_SESSION['ses_basket_price2']);

// Or your variables' values
$ses_basket_name[0] = '';
$ses_basket_amount[0] = 0;
$ses_basket_price[0] = 0;
$ses_basket_price2[0] = 0;

?>
The condition can be something like:
Code:
<?php

// ...

if ($double==1){ 
   //update price for the same entry.
  $oldamount = $ses_basket_amount[$basket_position];
   $ses_basket_amount[$basket_position]++;
   $amount = $ses_basket_amount[$basket_position];
   $oldprice=$ses_basket_price[$basket_position];
   $newprice=($oldprice/$oldamount)*$amount;
   $ses_basket_price[$basket_position]=$newprice;
   $total=$total+($oldprice/$oldamount);        
   /*foreach($ses_basket_price as $v){          
      $total = $total + $v;         
   }*/

// Maybe here
// If $sesbasket_items is registered
// Then you can delete those values

if(isset($delete)) {
   $ses_basket_name[0] = '';
   $ses_basket_amount[0] = 0;
   $ses_basket_price[0] = 0;
   $ses_basket_price2[0] = 0;
}

// ...
// rest of the code here

?>
Hope it helps.
 
Back
Top