SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding variable to shopping cart array

    Hi I have a shopping cart that stores the item variable $size in the cart array. I want to add other variables to this array to be put in the cart. Can anyone help me please, the coding is as follows:
    session_start();

    @ $new = $_GET['size'];

    if($new)
    {
    //new item selected
    if(!isset($_SESSION['cart']))
    {
    $_SESSION['cart'] = array();
    $_SESSION['items'] = 0;
    $_SESSION['total_price'] ='0.00';
    }

    if(isset($_SESSION['cart'][$new]))
    $_SESSION['cart'][$new]++;
    else
    $_SESSION['cart'][$new] = 1;

    // $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
    }

    if(isset($_POST['save']))
    {
    foreach ($_SESSION['cart'] as $size => $qty)
    {
    if($_POST[$size]=='0')

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi Yarb, welcome to Sitepoint Forums.

    Im sorry, but I don't understand what you are trying to do (which im guessing is the same for other members, otherwise it would be answered by now).

    Can you please explain in a bit more depth what your problems/aims are?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I am passing variables from a form to the cart.

    I am passing variables from a form to the cart. There are more variables then just $SIZE that I want to add into the cart. For example the colour, amount and frame. How can I add multiple variables for one item to the cart? I hope this helps.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, that will require something quite different from what you have there.

    Does your product system include a product-code or ID? or is Size the way you distinguish between products?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Well, since $_GET['size'] would get whatever value was passed from the form from an element that was named "size", if you just add different form elements to get what you need, that would allow you to get them.

    So, for example:
    Code:
    <form action="process_cart.php" method="get">
        <input name="size" id="size" type="text" />
        <input name="color" id="color" type="text" />
        <input name="amount" id="amount" type="text" />
        <input name="frame" id="frame" type="text" />
        <input type="submit" value="Submit" />
    </form>
    In your PHP script (which I named process_cart.php up there), you would then be able to call $_GET['size'], $_GET['color'], $_GET['amount'], and $_GET['frame'].

    Also, just a quick note, since this is a shopping cart which should be fairly secure, if you have no explicit reason to use 'get', you should use 'post'. It acts roughly the same, but it doesn't pass all the values in the URL in the browser. To use it, just set method="post" and use $_POST instead of $_GET.

    Hope that was what you were aiming for.

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, but the issue is where to store the variables?

    The present cart setup only allows for two variables, item and qty.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    At the moment the only id is the size

    That is correct arkinstall, I am glad you understand. At the moment the only id is the size.

  8. #8
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Heellp!

    As mentioned I am having trouble adding more than one variable to the cart for each item. Could someone lead me to a tutorial or explain how I could do this PLEASE It is driving me mental!

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can I have a better explaination of the data we're talking about here please? Im not sure whether you have product codes, prices, qtys, etc.

    Thanks,

    Jake Arkinstall
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I pass the item name which is the $size, the frame which could equal 5X7, cost which could equal 4.00, and the colour which could equal black & white. I hope this helps

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hmm...There is a bit of an issue.

    The way I would suggest is to store all of the details in an array, but then the typical way of adding an option would become tricky, because some people may want same sizes, but with different options.

    I'll get working on it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes so you see my problem.
    Just out of curiousity if you passed options as an array size[]. To print this you would need to add a loop. How do you add a loop within the cart array? So it can read all of the options in the size array as well as print each item in the cart array?

  13. #13
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Just wondering if anyone could help me with my problem of adding multiple parameters to the cart array. Please

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To be honest, the only way I can think of doing it is this: Assign a unique ID to all of the possible frame options, in a separate database table.
    For example:
    Code:
    Combinations Table
    Id     Size      Frame     Color    Cost
    1      item1     1x1        black    1.00
    2      item1     1x1        white    1.00
    3      item1     2x2        black    1.99
    4      item1     2x2        white    1.99
    5      item2     1x1        black    2.50
    6      item2     1x1        silver    2.65
    etc...
    and store the session as you already are, but rather than assigning $size to the cart key, assign the combination ID instead. This will be much more flexible, but could take time to make all of the combinations.

    But you could always do what I do - loop through all of the options and generate a mysql_query to complete the whole table data.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if I have the item in an array like so
    $new = array($size, $frame, $colour);
    normally you would display this like
    echo '|'.$new[0].'|'.$new[1].'|'.$new[3].'|';

    how do I put the whole $new array into the cart shown above?

  16. #16
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or do I use a multidimensional array because I am storing more than one piece of information?


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
  •