Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Shopping cart using sessions---suggestion needed please.

Links for php scripts

Moderators: macek, egami, gesf

Shopping cart using sessions---suggestion needed please.

Postby balong3030 » Wed Jun 29, 2005 2:37 am

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: Select all
<?
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>
balong3030
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Jun 27, 2005 4:27 am
Location: Manila

Postby gesf » Wed Jun 29, 2005 3:19 am

First of all try assigning values directly to a session variables, like: $_SESSION['order_qty'] = 'whatever'; instead of register it first with session_register();.
User avatar
gesf
Moderator
Moderator
 
Posts: 1718
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Postby balong3030 » Wed Jun 29, 2005 3:31 am

thanks gesf...i'll try that, be back in a moment.
balong3030
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Jun 27, 2005 4:27 am
Location: Manila

Postby balong3030 » Wed Jun 29, 2005 3:38 am

the order_qty comes from the form before this page.

Code: Select all
      <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?
balong3030
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Jun 27, 2005 4:27 am
Location: Manila

Postby gesf » Wed Jun 29, 2005 5:16 am

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 !?
User avatar
gesf
Moderator
Moderator
 
Posts: 1718
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Postby balong3030 » Thu Jun 30, 2005 1:31 am

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.
balong3030
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Jun 27, 2005 4:27 am
Location: Manila

Postby balong3030 » Fri Jul 01, 2005 12:44 am

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: Select all
<?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>";
?>
balong3030
New php-forum User
New php-forum User
 
Posts: 8
Joined: Mon Jun 27, 2005 4:27 am
Location: Manila

Postby gesf » Fri Jul 01, 2005 8:25 am

Got it.
Will take a better looking at the code as soon as possible!
User avatar
gesf
Moderator
Moderator
 
Posts: 1718
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Postby gesf » Sun Jul 03, 2005 6:47 pm

Just make some condition for the deletion part and then use the next code:
Code: Select all
<?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: Select all
<?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.
User avatar
gesf
Moderator
Moderator
 
Posts: 1718
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron