PHP session count problem
Can anyone see a problem with this code? Or perhaps advise with what else is required!
I have a basic shopping cart, as I only have a few products it's only basic script. A session is created, DB is accessed and displays products in a table on the screen.
When the user clicks a product in the table to add to basket it uses the following:
echo '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a>';
'
That takes them to cart.php and it all works. It accesses the DB and adds whatever the $id matches from the DB to the session.
Go back to the products page and it correctly adds more products to the basket.
It all works except the following code:
function basketquantity() {
$itemcount = $_SESSION['cart'];
if (!$itemcount) {
return 'Your Basket (0 Items)';
} else {
// Parse the cart session variable
$count = explode(',',$itemcount);
$s = (count($count) > 1) ? 's':'';
return 'Your Basket ('.count($count).' item'.$s.')';
}
}
$_SESSION['cart'] is the session used throughout the basket contents.
That above code is supposed to explode items in the session to get the actual number of items.
and if no items in basket (session) it correctly displays "Your Basket (0 items)"
when one item in basket (session) it correctly displays "Your Basket (1 item)"
but it wont show any more than 1 item. add more than 1 item to the basket and it still only shows "YourBasket (1 item)"
I have a large dent in my wall now - and my head hurts , so any help is appreciated!
Cheers
kb posted this at 14:07 — 29th June 2007.
He has: 1,380 posts
Joined: Feb 2002
Well, I would assume that if it only screws up when theres more than one item... then it would have to be:
<?php
$count = explode(',',$itemcount);
$s = (count($count) > 1) ? 's':'';
return 'Your Basket ('.count($count).' item'.$s.')';
?>
...right?
Here's a question.... when you do explode(','....); does that assume that you have a comma after every item? Does that follow through your whole code? So if you have two items, it looks like:
whatever,whatever,
...because that's what I would assume. And if that's the case, then you may want to check to make sure you follow that logic throughout.
(side note, I would never have known what "(count($count) > 1) ? 's':'';" meant because of how you structured it if it wasn't for knowing a fair amount of C... I had no idea that structure/syntax was supported in PHP?...)
Other than your potential logic problem, I don't see any glaring issues. Everything seems to revolve around the explode statement, so I would examine that and it's associated session data closely...
Hopefully that helps a little bit. If not, come back and we'll try again.... Good luck!
greg posted this at 17:53 — 5th July 2007.
He has: 1,581 posts
Joined: Nov 2005
thanks for the reply
I am unsure to be honest. I am still learning about sessions/arrays and functions
(variables are only just sinking in)
when i print the array (session) it looks like this
Array ( [2] => 11 [1] => 1 )
[2] is the id in mysql database 11 is the current quantity ($quantity)of that item in the cart (or session)
so [1] is id one in the database and currently there is only one of those items in the basket
the id from mysql is gained from a simple link:
Add to cart';
this is within the while command:
while($row = mysql_fetch_array( $result ))
(basically while is to print all the products in the database to the table)
then
$product_id = $_GET[id];
and there is a
switch($action) {
case "add": $_SESSION['cart'][$product_id]++;
case delete and remove all items, which both use unset, either unset [$product_id] ==0 or unset the whole $_SESSION['cart']
I just dont know
It seems somehow as if the function i have (as per first post) is checking the session for null or notnull?!?
I think that because it works for 0 items (session is empty) and works for 1 items
but more than one item it doesnt work
regardless of if its 3 of one product (mysql id 1) or 2 of one product and 1 of another
So you maybe right about the way i have stated the session items are speperated, but i have no idea how to determine what would be correct
also, to me, I would have thought :
$itemcount = $_SESSION['cart'];
which is the bit of the function i am having problems with, would need to be more
like it should specify somehow the actual quantity. I mean, how does it know to take the quantity numbers form all products stored in the session?
Cheers
kb posted this at 18:52 — 5th July 2007.
He has: 1,380 posts
Joined: Feb 2002
Ok, I don't know if this will get you anywhere, but as far as I know, you should use
<?php
$product_id = striptags($_GET['id'])
?>
I would stop using the "explode" statement. Because (unless I'm blind), I don't see anywhere that you're adding commas. As far as I can tell, you're just pulling info from a session, and then looking at the numeric value inside. Right?
So what I would do is just write a loop to pull out all of those values, and work with that. So maybe you have
<?php
$cart_arr = array();
$count = 1;
?>
and then use $cart_arr to store those values pulled from the session, leaving NULL values where there aren't any requests... but do it sequentially. So if you had values for item id 1 and 2, but that was it, your array would look like
<?php
$count_arr[1] = whatever;
$count_arr[2] = whatever;
$count_arr[3] = NULL;
?>
and then because you have a specific count (aka number) of things, you know where to stop.
I can't really write anything in total for you because I don't have all your code, and I don't fully understand your layout, but I think my suggestion should work. Get rid of the 'explode' and just use some sort of counting system instead... because you don't ever insert any commas anywhere.
See what you can do, and come back with some more. We'll go from there.
greg posted this at 16:51 — 6th July 2007.
He has: 1,581 posts
Joined: Nov 2005
thanks brady.k for the interest and help and most definitely the ideas
I have tried a whole new approach and it works fine
(looks like variables have become more clear to me than I thought)
You saying to forget the explode gave me the idea to loop and get all session data and add it to a variable within the loop
Then just use that variable in the simple if/else.
Sincerely thanks for the help, but as is often mentioned on this and other forums, there is nothing more rewarding than solving something yourself.
Cheers
kb posted this at 18:20 — 6th July 2007.
He has: 1,380 posts
Joined: Feb 2002
Amen! I'm glad I could be that spark... funny, that's what I was trying to do....
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.