raptor30506090 Posted May 1, 2012 Share Posted May 1, 2012 Hi guys help please foreach($_SESSION['cart'] as $id => $value){ } is this valid? im getting error Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shopping cart\new_cart.php on line 48 Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/ Share on other sites More sharing options...
Adam Posted May 1, 2012 Share Posted May 1, 2012 foreach requires an argument that can be iterated through, like an array or object. I'm guessing $_SESSION['cart'] is not being set as you're expecting. Try passing the $_SESSION array into var_dump to see what the cart key is. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1341977 Share on other sites More sharing options...
batwimp Posted May 1, 2012 Share Posted May 1, 2012 Did you remember to call session_start() at the top of the page? Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1341978 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 yes session start is at the top Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1341981 Share on other sites More sharing options...
El Chupacodra Posted May 1, 2012 Share Posted May 1, 2012 It's most likely what Adam is after - that $_SESSION['cart'] isn't an array. Check it with gettype() or is_array(). Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1341994 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 or var_dump or print_r to see what it contains as well as the type. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1341997 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 ok here is the full code if that helps if(isset($_GET['id'])){ $id = $_GET['id']; if(isset($_GET['action'])){ $action = $_GET['action']; }else echo $action="Empty Cart" ; switch($action){ case 'add': if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]++;} else $_SESSION['cart'] =1; break; case 'remove': if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]--; if($_SESSION['cart'][$id]==0){ unset($_SESSION['cart'][$id]); } } break; case 'delete': unset($_SESSION['cart'][$id]); break; }} ////////////////////////////// Display Shopping Cart ///////////////////////////////// if(isset($_SESSION['cart'])){ $total = 0; foreach($_SESSION['cart'] as $id => $value){ } $result = mysql_query("SELECT * FROM products WHERE id=" . $id)or dei(mysql_error()); $row = mysql_fetch_array($result); $price = $row['price']; $line_cost = $price * $value; $total = $total+ $line_cost; }print_r($_SESSION['cart']); Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342003 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 full code to pay with Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342015 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 Any help please i hate posting to my self Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342030 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 You should apply the advice posted in this thread. What displays when you print_r or var_dump $_SESSION immediately before you get the error. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342033 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 Sorry about that im getting Array ( [cart] => 1 [cart_1] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342052 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 So, clearly $_SESSION['cart'] is not an array. The first few lines in your add case might be the problem. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342056 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 So you understand why your foreach loop was failing then? foreach( 1 as $id => $value ) You need to make sure $_SESSION['cart'] is an array before looping through it. case 'add': if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]++;} else $_SESSION['cart'] =1; break; is probably the issue. jesi beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342057 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 Again, cause I am a ninja! Or just need more stuff to do. *wanders off* Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342060 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 Possibly, or it could be the fact that my post was more complete? SNAP! Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342061 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 Possibly, or it could be the fact that my post was more complete? SNAP! complete, verbose, whichever Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342071 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 ok iv made it array im now getting this Array ( [cart] => 1 ) but i still have this Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shopping cart\new_cart.php on line 49 thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342082 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 $_SESSION['cart'] is still 1. 1 is an integer, and can not be looped through using foreach. We've told you where the issue is, it's up to you to ask the right questions, or fix it. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342085 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 ok as you can see i need help not done this befor so were am i going wrong hope thats the right question Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342086 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 Array ( [cart] => 1 [cart_1] => 7 [cart_] => 4 ) Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shopping cart\new_cart.php on line 50 now im getting this Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342099 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 Do you know what an array is? Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342102 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 an array can store many items i am very new with this not tryed to do a shopping cart before Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342104 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 $something as $key => $value Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342111 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 Okay so see how cart = 1? That's not an array. An array would be like [cart] => Array ( [qty] => 1 [id] => 7) We already told you where the problem is, and another person even posted the section of code that is the problem. Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342117 Share on other sites More sharing options...
raptor30506090 Posted May 1, 2012 Author Share Posted May 1, 2012 ok but iis it not easy just to tell me how to fix it so i now for next time? Quote Link to comment https://forums.phpfreaks.com/topic/261904-session-help/#findComment-1342120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.