SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 37
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Transferring Sizes Into My Shopping Cart

    Hi guys,

    I really need your help because I am truly stumped. At the minute I have your basic shopping cart, items are added up and if you add multiple items it changes the quantity field to match and it looks like so...



    However now I want to transfer sizes into the checkout as well, I already have the form set up when you browse the item and it transfers a variable called $Size (containing values like 'Small', 'Medium' etc...) to the lib file where all the information submitted is then transformed into information for the checkout...

    PHP Code:
    if(!isset($HTTP_SESSION_VARS['cart'])){
        
    $HTTP_SESSION_VARS['cart']=array();
        
    $HTTP_SESSION_VARS['items']=0;
        
    $HTTP_SESSION_VARS['total']="0.00";
    }

    if(
    $add){

        
    $itemID $HTTP_POST_VARS['product'];
        
    $isFound false;
        for(
    $i =0$icount($HTTP_SESSION_VARS['cart']);  $i++){
            if(
    $HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
                
    $isFound $i;
                break;
            }
        }

        if(
    $isFound !== false){
            
    $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
        }else{
            
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
        
            
            
    array_push($HTTP_SESSION_VARS['cart'], $newItem);
        }
        
         
    $HTTP_SESSION_VARS['items']++;
        
    // array_push($HTTP_SESSION_VARS['cart'], $product);
         
    $HTTP_SESSION_VARS['total']+=$price;
     } 
    However the way my cart is set up is that it doesn't show the multiple items of the same type seperately instead it puts a 2 in the quantity field...



    ...firstly I need to change that and then I need to figure out a way of creating it so that it looks like this:



    My checkout code looks like this:

    PHP Code:
        $subtotal 0;
        
        for(
    $i=0$i<count($myCart); $i++){
            
    $query="SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";
            
    $result=mysql_query($query$conn);
            
    $basketItems=mysql_fetch_array($result);
            
    $subtotal+=($basketItems['UK_Price']*$myCart[$i]['quantity']);

    //ITEM NAME

    echo $basketItems['Item_Name'];

    //QUANTITY

    echo $myCart[$i]['quantity'];

    //PRICE (exc VAT)

    echo money_format($fmt, ($basketItems['UK_Price']*$myCart[$i]['quantity'])); 
    This is quite a long winded request, but as much or as little help would be great. Any further information then please just ask.

    P.S. Some of the details on the screenshots are wrong, but you get the jist from the explanations.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...I recon you are putting the items into a session... When you put the items into the session you might want to make sure it's an array.. Then explode the array and use a foreach function to display all the items.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think thats what Im doing here is it not...

    Code:
        if($isFound !== false){ 
            $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++; 
        }else{ 
            $newItem = array(); 
            $newItem['Item_ID'] = $itemID; 
            $newItem['quantity'] = 1;
    The thing is if I add something like:

    $newItem['Size'] = $Size;

    ...to it, it works kind of, but the way the checkout works is that if I put "echo $myCart[$i]['Size'];" in where I want the size to appear it works when I have one item in there, however when you add more than 1 item of different types it seems to think that you've added another of the same size...

    e.g. If I add 1 Adult Away Shirt that is Extra Large, then add another but this time it's small it appears in the checkout as...

    Adult Away Shirt 2005 / 2006 Extra LargeExtra Large

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *Bump*

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do the different sizes of an item have different id's?

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean in the form itself? The values they have in the form are things like value="Small", value="Medium" etc....

    I did have it as value="1", value="2" but I changed it. It can easily be changed back if that would help?

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form looks like this...

    PHP Code:
    if ($myrow['Item_Quantity'] == 0)        
                 {
    echo 
    '<select name="Size" size="1">';
            
            if (
    $myrow['YS_Quantity'] > 0) { 
            echo 
    "<option selected value=\"Youth Small\">Youth Small</option>"
            } 
            if (
    $myrow['YM_Quantity'] > 0) { 
            echo 
    "<option value=\"Youth Medium\">Youth Medium</option>"
            } 
            if (
    $myrow['YL_Quantity'] > 0) { 
            echo 
    "<option value=\"Youth Large\">Youth Large</option>"
            } 
            if (
    $myrow['YXL_Quantity'] > 0) { 
            echo 
    "<option value=\"Youth Extra Large\">Youth Extra Large</option>"
            } 
            if (
    $myrow['Small_Quantity'] > 0) { 
            echo 
    "<option selected value=\"Small\">Small</option>"
            } 
            if (
    $myrow['Medium_Quantity'] > 0) { 
            echo 
    "<option value=\"Medium\">Medium</option>"
            } 
            if (
    $myrow['Large_Quantity'] > 0) { 
            echo 
    "<option value=\"Large\">Large</option>"
            } 
            if (
    $myrow['Xlarge_Quantity'] > 0) { 
            echo 
    "<option value=\"Extra Large\">Extra Large</option>"
            } 
            if (
    $myrow['XXlarge_Quantity'] > 0) { 
            echo 
    "<option value=\"Extra Extra Large\">Extra Extra Large</option>"
            } 
            
    echo 
    '</select>';
            } 

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have this in your code:

    $itemID = $HTTP_POST_VARS['product'];

    and I want to know if the different sizes of an item have a different $itemID.

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh right, no Im afraid they don't it looks like this in the database:

    http://img318.imageshack.us/img318/5435/database0ja.jpg

    Each Item has a unique Item_ID, but not every size.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is if I add something like:

    $newItem['Size'] = $Size;
    Ok, then you could create an array to hold the different sizes. Something like:
    Code:
    $newItem['Size'][$Size]++;
    That will create an array called $newItem['Size'] which will hold all the sizes. You have to make sure that any time an item is added to the cart that the size is recorded in that array.

    You can use a foreach() loop to loop through that array to output the correct number of item lines in your checkout display.

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for the help mate...

    So for example my current loop which gets the information for the check out looks like this:

    PHP Code:
        for ($i=0$i<count($myCart); $i++){
            
    $query="SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";
            
    $result=mysql_query($query$conn);
            
    $basketItems=mysql_fetch_array($result);
            
    $subtotal+=($basketItems['UK_Price']*$myCart[$i]['quantity']);

    //ITEM NAME 

    echo $basketItems['Item_Name']; 

    //QUANTITY 

    echo $myCart[$i]['quantity']; 

    //PRICE (exc VAT) 

    echo money_format($fmt, ($basketItems['UK_Price']*$myCart[$i]['quantity']));


    It doesn't work when I change that to a foreach ($i=0; $i<count($myCart); $i++){ etc...etc... Forgive me as I don't know much about the foreach loop. But by just reading a bit about it, it seems it has to look something like:

    ($months as $month)

    How would I change my current query to be able to use the foreach loop?

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The array is not in your database. The array is in your php session variable.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    The array is not in your database. The array is in your php session variable.
    Right... My lib file now looks like this:

    PHP Code:
    if($add){

        
    $itemID $HTTP_POST_VARS['product'];
        
    $isFound false;
        for(
    $i =0$icount($HTTP_SESSION_VARS['cart']);  $i++){
            if(
    $HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
                
    $isFound $i;
                break;
            }
        }

        if(
    $isFound !== false){
            
    $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
        }else{
            
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
                 
    $newItem['Size'][$Size]++;
            
            
    array_push($HTTP_SESSION_VARS['cart'], $newItem);
        }
        
         
    $HTTP_SESSION_VARS['items']++;
        
    // array_push($HTTP_SESSION_VARS['cart'], $product);
         
    $HTTP_SESSION_VARS['total']+=$price;
     } 
    So Ive added the array you suggested, thats right isn't it? Its just getting it to display in the checkout now. So how would I go about doing that, sorry if Im being a pain in the ****, just that Im slowy picking things up.

  14. #14
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't work when I change that to a foreach ($i=0; $i<count($myCart); $i++){ etc...etc... Forgive me as I don't know much about the foreach loop. But by just reading a bit about it, it seems it has to look something like:

    ($months as $month)
    PHP Code:
    $item['size']['small'] = 3;
    $item['size']['medium'] = 1;
    $item['size']['large'] = 2;

    foreach(
    $item['size'] as $key=>$value)
    {
        echo 
    "$key : $value <br/>";


  15. #15
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to make sure that any time an item is added to the cart that the size is recorded in that array.
    My lib file now looks like this:
    PHP Code:
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
                 
    $newItem['Size'][$Size]++; 
    What if the item is already in the cart?

  16. #16
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is not what it should look like? How should it look?

    It's just that you've given me this advice but I dont know how and where to use it.

  17. #17
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also have to take some steps to add the size when the item is already in the cart.

  18. #18
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *double post - see below*

  19. #19
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    You also have to take some steps to add the size when the item is already in the cart.
    Ok then, lets just focus on this for now then...

    This is where the form is posted so this is where we it adds things to the cart

    PHP Code:
    if($add){

        
    $itemID $HTTP_POST_VARS['product'];
        
    $isFound false;
        for(
    $i =0$icount($HTTP_SESSION_VARS['cart']);  $i++){
            if(
    $HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
                
    $isFound $i;
                break;
            }
        }

        if(
    $isFound !== false){
            
    $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
        }else{
            
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
            
            
    array_push($HTTP_SESSION_VARS['cart'], $newItem);
        }
        
         
    $HTTP_SESSION_VARS['items']++;
        
    // array_push($HTTP_SESSION_VARS['cart'], $product);
         
    $HTTP_SESSION_VARS['total']+=$price;
     } 
    You said that I was adding $newItem['Size'][$Size]++; in the wrong place beforehand so where should it go so that if an item is already in the cart it takes the relevant steps.

    Would this work?

    PHP Code:
    if($add){

        
    $itemID $HTTP_POST_VARS['product'];
        
    $isFound false;
        for(
    $i =0$icount($HTTP_SESSION_VARS['cart']);  $i++){
            if(
    $HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
                
    $isFound $i;
                break;
            }
        }

        if(
    $isFound !== false){
            
    $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
                     
    $HTTP_SESSION_VARS['cart'][$isFound]['Size']++;
        }else{
            
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
                     
    $newItem['Size'][$Size]++;
            
            
    array_push($HTTP_SESSION_VARS['cart'], $newItem);
        }
        
         
    $HTTP_SESSION_VARS['items']++;
        
    // array_push($HTTP_SESSION_VARS['cart'], $product);
         
    $HTTP_SESSION_VARS['total']+=$price;
     } 

  20. #20
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would this work?
    Why are you asking? Test it.

    One problem you might consider is: do all items have sizes? What if an item doesn't have different sizes? How will you handle that?

  21. #21
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well because some items don't have sizes I was hoping to use the same thing Ive got going on now because that works without any sizes and automatically incremements the quantity depending on how many you add.

    So somehow Ive got to create a cart so that when sizes are posted to the lib file it puts items which are the same but with different sizes on different rows but also items which are the same but ALSO of the same size to automatically incremement the quantity.

    Oh and for the record now it doesn't work I kept getting messages saying " Illegal Offset Type", and when I tried putting:

    PHP Code:
    $item['size']['small'] = 3
    $item['size']['medium'] = 1
    $item['size']['large'] = 2

    foreach(
    $item['size'] as $key=>$value

        echo 
    "$key : $value <br/>"

    ...in the checkout it just printed out all the sizes.

  22. #22
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well because some items don't have sizes I was hoping to use the same thing Ive got going on now because that works without any sizes and automatically incremements the quantity depending on how many you add.
    How will you know whether you should increment the quantity or whether you should create a size array and increment the size?

  23. #23
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well because the sizes are held in a dropdown with the variable name $Size, the items without any sizes don't produce a dropdown, so I was thinking of adding something like...

    if($add && $Size) {
    Create a size array and increment the size...
    }
    if($add) {
    Increment the quantity
    }

  24. #24
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like a good plan.

  25. #25
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just finding a way of posting the sizes to the checkout that Im not succeeding in. I know you suggest creating an array which stores the variable $Size inside, but I honestly haven't a clue on how to do it.

    As much as Im grateful for your suggestions to do this that and the other, I don't know how and where to apply them. If you could start me off that would great i.e. Can you fit your suggestion about creating a $Size array into whats already here? I realise it won't be the same as what Im doing at the minute, but it can't be too disimilair can it?

    PHP Code:
    if($add){

        
    $itemID $HTTP_POST_VARS['product'];
        
    $isFound false;
        for(
    $i =0$icount($HTTP_SESSION_VARS['cart']);  $i++){
            if(
    $HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
                
    $isFound $i;
                break;
            }
        }

        if(
    $isFound !== false){
            
    $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
        }else{
            
    $newItem = array();
            
    $newItem['Item_ID'] = $itemID;
            
    $newItem['quantity'] = 1;
            
            
    array_push($HTTP_SESSION_VARS['cart'], $newItem);
        }
        
         
    $HTTP_SESSION_VARS['items']++;
        
    // array_push($HTTP_SESSION_VARS['cart'], $product);
         
    $HTTP_SESSION_VARS['total']+=$price;
     } 


Bookmarks

Posting Permissions

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