Samza Posted December 1, 2012 Share Posted December 1, 2012 (edited) Hi, I am following the PHP Academy tutorial on youtube, and I'm on part 4 (http://www.youtube.com/watch?v=cJJKQ8MXGrE). However, I am running into a bit of a problem as I have other session variables for my user login and when I am using the foreach statement I dont want to include the login session variables if you understand, only the product_ variables function cart() { foreach($_SESSION as $name => $value) { if ($value>1) { if (substr($name, 0, =='product_'){ $id = substr($name, 8, (strlen($name)-); echo '<br />'.$id; } } else { echo "Your cart is empty! <br />"; echo $name.': has a value of :'.$value; } } } and this is displayed: As you can see it's also using the login session variables which is causing some issues for me because it's displaying that the cart is empty when it is not! So i'm a bit stuck as to what to do :S Sam- Edited December 1, 2012 by Samza Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/ Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2012 Share Posted December 1, 2012 And do you have error_reporting at E_ALL, or preferably -1, and display_errors set to On? Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396704 Share on other sites More sharing options...
Samza Posted December 1, 2012 Author Share Posted December 1, 2012 (edited) And do you have error_reporting at E_ALL, or preferably -1, and display_errors set to On? I haven't manually set this myself so I'm guessing not. I am using xampp. Would this help ? Edited December 1, 2012 by Samza Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396705 Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2012 Share Posted December 1, 2012 In your php.ini file, find the error handling section and make sure the following lines are in it, and appear only once without a leading semicolon: error_reporting = -1 display_errors = On Then restart Apache and see if you get any errors when you run the script. If you find more than one php.ini file, put phpinfo(); die(); in a script and run it. You can also verify your settings that way. Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396707 Share on other sites More sharing options...
Samza Posted December 1, 2012 Author Share Posted December 1, 2012 Okay this didn't make a difference :S I think the problem is to do with the login session variables which are being compared against my variables for each product in a cart which is being added upon a hyperlink. Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396708 Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2012 Share Posted December 1, 2012 Add this line to your script and see if you get an error: $notvalid = $_SESSION['notvalid']; It should produce an 'undefined index' error. If it doesn't, we need to figure out why. Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396713 Share on other sites More sharing options...
Samza Posted December 1, 2012 Author Share Posted December 1, 2012 (edited) Add this line to your script and see if you get an error: $notvalid = $_SESSION['notvalid']; It should produce an 'undefined index' error. If it doesn't, we need to figure out why. I receive an no errors Edited December 1, 2012 by Samza Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396718 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2012 Share Posted December 2, 2012 The tutorial you found is, what would be a polite phrase, 'technically challenged'. The cart stored in the session should be stored in a unique session variable like $_SESSION['cart'], not in $_SESSION. This would separate any data for the cart from data for other purposes. Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396922 Share on other sites More sharing options...
Samza Posted December 2, 2012 Author Share Posted December 2, 2012 The tutorial you found is, what would be a polite phrase, 'technically challenged'. The cart stored in the session should be stored in a unique session variable like $_SESSION['cart'], not in $_SESSION. This would separate any data for the cart from data for other purposes. They get stored as a session named as " product_'productid' " so how can I specifically target sessions beginning with 'product_' ? Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396934 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2012 Share Posted December 2, 2012 (edited) how can I specifically target sessions beginning with 'product_' ? If you make the change I suggested, you don't have to target specific keys. All the 'cart' related keys/values will be in $_SESSION['cart']. All the references to $_SESSION that are part of the 'cart' logic just need to be changed to $_SESSION['cart']. Edit: Also, I would add that the definition of that cart, where you are using " product_'productid' " is overly complicated. You have probably 2-3 times more code than needed for every function that references the cart. Just use the product id as the key and store the quantity as the value, and if the quantity is zero, just remove the entire key/value from the cart. Edited December 2, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396935 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2012 Share Posted December 2, 2012 (edited) Sample code that demonstrates how simple your 'cart' and code should/can be - <?php session_start(); // if the cart does not exist, create an empty cart if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); } // fake adding items to the cart for demo purposes $_SESSION['cart'][1] = 3; // id = 1, qty = 3 $_SESSION['cart'][2] = 1; // id = 2, qty = 1 // lists only the id/qty in the cart function list_cart($cart){ if(empty($cart)){ echo "Your cart is empty! <br />"; } else { foreach($cart as $id=>$qty){ echo "Product Id: $id, Quantity: $qty<br />"; } } } list_cart($_SESSION['cart']); // example usage, pass the actual cart into the function as a call-time parameter Some notes about this code - 1) The 'cart' should be passed into the function, as shown, as a call-time parameter so that the 'cart' can come from anywhere, a session, a database query, a cookie, xml, ... 2) I named the function list_cart because that's all it does. To actually display all the information about the contents in the cart, you would extract the id's (using array_keys), use ONE query, not in a loop, to get the product information for all those product id's, store that product information into an array, again using the product id as the key to that array, then as you loop through the contents of the 'cart' you would use the product id from the cart to reference the product information for that id. 3) The variable names and array index names indicate the meaning of the data in the variables. Edited December 2, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271450-session-variables-problem/#findComment-1396948 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.