RegEx? Grabbing the number of each unique ID....
Posted: Tue Nov 11, 2008 12:18 am
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:
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
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'];
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