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 Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Having problems with arrays

    Hi,

    I am using an array to pass information into a shopping cart.

    I want to pass 3 bits of info into the array but only the first one is stored.

    Also when I've been sending a different item in its been overwriting the original one instead of having both.

    My code is:
    PHP Code:
    <?php

    require_once('inc/mysql.class.php');     // Include MySQL class
    require_once('inc/global.inc.php');      // Include database connection
    require_once('inc/functions.inc.php');   // Include functions
    session_start();                         // Start the session
    $action $_GET[action];          // The action from the URL
    $id $_GET[id];                  // The product id from the URL
    $size $_GET[size];
    $quan $_GET[quan];

    switch (
    $action) {
        case 
    'add':
            if (
    $cart) {
                
    $cart .= ','.$_GET[id];
                
    $cart .= ','.$_GET[size];
                
    $cart .= ','.$_GET[quan].';';
            } else {
                
    $cart $_GET[id];
            }
            break;
        case 
    'delete':
            if (
    $cart) {
                
    $items explode(',',$cart);
                
    $newcart '';
                foreach (
    $items as $item) {
                    if (
    $_GET['id'] != $item) {
                        if (
    $newcart != '') {
                            
    $newcart .= ','.$item;
                        } else {
                            
    $newcart $item;
                        }
                    }
                }
                
    $cart $newcart;
            }
            break;
        case 
    'update':
        if (
    $cart) {
            
    $newcart '';
            foreach (
    $_POST as $key=>$value) {
                if (
    stristr($key,'qty')) {
                    
    $id str_replace('qty','',$key);
                    
    $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                    
    $newcart '';
                    foreach (
    $items as $item) {
                        if (
    $id != $item) {
                            if (
    $newcart != '') {
                                
    $newcart .= ','.$item;
                            } else {
                                
    $newcart $item;
                            }
                        }
                    }
                    for (
    $i=1;$i<=$value;$i++) {
                        if (
    $newcart != '') {
                            
    $newcart .= ','.$id;
                        } else {
                            
    $newcart $id;
                        }
                    }
                }
            }
        }
        
    $cart $newcart;
        break;
    }
    $_SESSION['cart'] = $cart;

    $output[] = '<div id="items">';
    echo 
    showCart();
    $output[] = '</div>';
    ?>
    At the moment I'm just using direct urls to test it.

    I have a vague memory from a few years ago that in Java you can define what would be stored in an array and wonder if you can do that in PHP (I can't find anything that says you can) and whether it would help if you can.

    Many many thanks, I've been trying to fix this for a few days now!

    Flic

  • #2
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This seems like an overly complex solution...

    Why not make an entire array a session variable, and then use subarrays in that?

    PHP Code:
    $_SESSION['cart'] = array();

    if (
    $action == 'add') {
        
    $_SESSION['cart'][] = array('id' => $id'size' => $size'quantity' => $quantity); 
    While it can be a matter of opinion, I think that anytime you're using comma delimited strings to store data, you should look for something more structured to use.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hm, so I get the idea of this. But with the example I've seen with an array in an array explicitly gives the first array an entry, eg:

    PHP Code:
       $_SESSION['cart'][] = array("item" => array('id' => $id'size' => $size'quantity' => $quan)); 
    So I'm not sure how to use it straight from the session and making it loop. It would need to loop through the different entries to pick out the different items and am not sure how to do that with this!

    Thanks

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Something like
    PHP Code:
    foreach($_SESSION['cart'] as $item)
    {
        
    $id $item['item']['id'];
        
    $size $item['item']['size'];

    for example.


  •  

    Posting Permissions

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