I have this php cart code below. For some reason it keeps saying Notice: Undefined variable: available in C:\xampp\htdocs\test\cart.php on line 47 I think the problem source is might be the foreach loop but I’m not sure.
Well $available is defined with the foreach but it’s in an if condition, so won’t be defined if the condition is not met.
Looking at this, I can’t help thinking that the way you are storing data is not the most useful. But hard to say without knowing what you are working with. But the use of json_decode and explode suggest a database redesign may help.
Actually this is the first time I do something like this so I searched for some tutorial on youtube. The guy explains things very well while coding. I did the same he did but it not works for me. Link
It would help if you could tell us which is line 47 (ie the line where the error is). As it says in comments to the tutorial, if you don’t get the same results, it will usually be a spelling mistake somewhere.
I’m tempted to say your tutorial! However, if you added one line
$available = 0;
before that if statement, it would at least mean you shouldn’t get an undefined error. Not knowing the tut I have no idea whether it will give the required result.
Yes, the if condition seems to accept there is a possibility that the condition may not always return true, but it makes no allowance for that happening, as if it’s a given that availablewill always have a value assigned.
There are a few ways to handle this. Set an initial value as @Gandalf suggests, inset an else after the if or use an if(isset($available)) condition before referencing the variable. The best way depends on the context of how the script is supposed to work.
But I could not bring myself to watch the whole of that tutorial to see exactly how it should work. Though from what I did see of it, I did not rate it very much.
ok, I’ve tried to add something like this after the if
else{ $available = $s[0];
}
the error message is gone, but it not working as it should be. if I add to the cart 3 items from one of the size option which has 7 items set in it there should be 4 items left so I should be able add 4 more in the cart with the + button. but I can’t because it shows “Max”.
By the way, here is a var_dump from my shopping cart. Some other guy pointed out for me. I’m using accentuated letters in my sizes option. “Elsu0151 opció” is should be “Első opció”. Is it possible that it causes the problem? The var_dupm says it has 21 characters but it has only 14 (15 if count the ‘space’)
That’s because you’ve assigned the “available” value to the size, rather than to a suitable value. As you’re comparing $s[0] to $item['size'], that suggests that it’s values will be text strings, from your var_dump above. So that’s not going to work really well for calculating the number of items available. I believe it will evaluate to zero, though, so you might as well just set it to zero as @Gandalf said earlier and make things less confusing.
How does the var_dump of the shopping cart, in particular the “size” field, compare to a var_dump of the $s array that you create, or indeed the $item['size'] field? From your last post it does seem that there is a character-encoding issue, but if both arrays have the same encoding issue then that would not cause it.
The issue boils down to the fact that the text in your shopping cart in the “size” field does not match any of the options for that field that come from the database record. As I imagine that the shopping cart is created from that database, that suggests that when the data is stored in the cart, something changes. So you need to look in your “update_cart()” function to see what that is doing.
Your code should deal with the data in the shopping cart being corrupted or not matching the database though.