Page 1 of 1

RegEx? Grabbing the number of each unique ID....

Posted: Tue Nov 11, 2008 12:18 am
by Frozenlight777
I'm creating a really tiny shopping cart application (not for real use) and I'm a little stumped on how to add to the quantity for each unique item.

I can get the total number of items in the cart, and count that. But when I have more than 1 of the same item I'm lost on how to count it specifically. Right now I have:

Code: Select all

 
echo "<a href=index.php?action=add&id=" . $row[pd_id] . "&item=" . $row[pd_name] . "><img src=images/add_to_cart.gif border=0></a>";
 
    $cart = $_SESSION['cart'];
    if ($cart) {
        $cart .= '.'.$_GET['id'];
    } else {
        $cart = $_GET['id'];
        $cost = $_GET['cost'];
    }
    $_SESSION['cart'] = $cart;
    $items = explode('.',$cart);
 
    echo '<p>Proceed with Checkout you have <a href="cart.php">'.count($items).' items in your shopping cart</a></p>';      
    echo $_SESSION['cart'];
 
 
The result of $_SESSION['cart'] is just a long string of concatenated product IDs like this
1,1,1,1,1,3,3,2,2,2,2,2,1

So my question is, how could I count each ID
6 - 1s
5 - 2s
2 - 3s


Thanks in advance

Re: RegEx? Grabbing the number of each unique ID....

Posted: Tue Nov 11, 2008 4:11 am
by Ziq
May be, you will be interested array_count_values().

P.S. I don't think that this is the best solution. If someone buy 100 pieces of 100 items it's 10000 ids in session variable...

Re: RegEx? Grabbing the number of each unique ID....

Posted: Tue Nov 11, 2008 5:59 am
by Stryks
I always find it interesting that people tend to want to complicate the uncomplicated. In this case, why you would want to create an array of values as a concatenated string and then convert it back to an array every time you want to use it.

If you create an array in your session (as a multi-dimensional array) it gets serialized automatically between page views, so what you are doing is done by default. Except that not doing it manually means that you don't have to manually undo it to use it.

So consider a cart item as a single dimensional array, as you have done in your code.

Code: Select all

$cart_item = 4;
You're only carrying the item id in that array, so it's a bit limited as to being able to tell you how many you have of them. So, a multi-dimensional array ...

Code: Select all

$cart_item = array('id' = 4, 'qty'=>1);
Ok .. so we can have multiple variables for one item. But we want more than one item.

Code: Select all

$cart_item = array('id' = 4, 'qty'=>1);
$cart[] = $cart_item;
 
// which would allow use to go right on into 
$cart_item = array('id' = 6, 'qty'=>4);
$cart[] = $cart_item;
 
// which would give us $cart containing two arrays, each containing the id and quantity of product.
But, we want to piggyback that onto the session, so ...

Code: Select all

$cart_item = array('id' = 4, 'qty'=>1);
$_SESSION['CART'][] = $cart_item;
So for each time you pass the second line above, a new cart id is created containing an array if the item data added. Best of all, this data is totally accessible and ready for use at any time.

If this looks like it might be of help, take a look at THIS POST to get an idea of how to work with cart data in the proposed format.

Hope this helps though I didn't really directly answer your question.