Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation shopping cart using sessions

    I've revised my previous code and followed and modified sample code at a tutorial. the code is working in terms of adding. but i could not get it to delete and update quantity. suggestions pls.

    here is the code. $price, $price2, $id, came from a link on a page.
    e.g. <A HREF="minibasket3.php?id=1 & price=100 & price2=100 & basket=key">
    PHP Code:
    <?php
    session_start
    ();
    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?
    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");
        }
    }
    ?>
    <!--// show basket-->
    <table border=1>
    <form action="<? $PHP_SELF?>" method="GET">  
    <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;
    ?>      
          <tr>
          <td><input type="text" name="quant" size="3" value="<? echo $amount?>"></td>
          <td><? echo $name?></td>
          <td><? echo $price2?></td>
          <td bgcolor=#eeeeee><? echo $price?></td>
          <td> <input name="act" type="hidden" value="upd"> </td>
          <td> <input type="submit" name="submit" value="Update"></td>
          <td> <a href="<? $PHP_SELF ?>?action=delete"> Remove </a></td>
          </tr>
            <? if($action=="delete"){
                
    $total=$total-$price2;
                unset(
    $ses_basket_price2[$basket_counter]);
                unset(
    $ses_basket_price[$basket_counter]);
                unset(
    $ses_basket_amount[$basket_counter]);
                unset(
    $ses_basket_name[$basket_counter]);
                }
                
    ?>
    <? 
    ?>
    <? 
        
    } else {
        
    $ses_basket_items=0;
        unset(
    $ses_basket_name);
        unset(
    $ses_basket_amount);
        unset(
    $ses_basket_price);
        unset(
    $ses_basket_id);
    }
    ?>
    <tr><td colspan=3> total price is: </td><td bordercolor=#ff0000><? echo $total;?></td></tr>
    <tr><td colspan="4"><a href="store2.php"> continue shopping </a></td></tr>
    </form>
    </table>
    </body>
    </html>
    Last edited by balong3030; 07-02-2005 at 01:41 PM.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,768
    Thanks
    19
    Thanked 155 Times in 146 Posts
    the code is working in terms of adding. but i could not get it to delete and update quantity.
    Could you be a little more specific with the problem you're having...and could you please put all that code between some [PHP] tags....
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    re: shopping cart

    the problem is deleting in array.

    if I click on remove, it will remove everything in the ff arrays:

    These is the block it goes through when deleting:
    PHP Code:
                <? 
                 
    if($action=="delete"){
                 
    $total=$total-$price2;
                 unset(
    $ses_basket_price2[$basket_counter]);
                 unset(
    $ses_basket_price[$basket_counter]);
        unset(
    $ses_basket_amount[$basket_counter]);
        unset(
    $ses_basket_name[$basket_counter]);
        }
        
    ?>
    I'm expecting it to remove only the rows displayed but instead it deletes everything in the array including other rows.
    Last edited by balong3030; 07-02-2005 at 01:56 PM.

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its not only deleting the row in line with the remove link, but it also removes the row above it. And the $total is returning zero which is not supposed to .... not unless there are no other items in the ff arrays.
    Last edited by balong3030; 07-02-2005 at 01:58 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •