Freedom-n-Democrazy Posted October 6, 2011 Share Posted October 6, 2011 if ($content['size'] == "sizel") {$content['size'] = "Large";} echo "{$content['size']}"; This doesn't work and just echo's "sizel". Why? Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/ Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 Is that the whole code section? Have you tried a var_dump on the $content['size'], it may contain a white space. Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276302 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Author Share Posted October 6, 2011 This is the whole code in the section: if ($content['size'] == "sizel") {$content['size'] = "Large";} if (empty($_SESSION['cart']['content'])) { echo "No product in cart."; } else { foreach ($_SESSION['cart']['content'] as $content) { echo "Product ID: {$content['id']}<BR>{$content['size']}<BR><BR>"; } } When you say white space, what do you mean by this? A space that should have not been some where? Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276307 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 I see whats happening, you will need to run your IF statement inside the section that is creating the $_SESSION. The if (isset($_GET['add']) blah blah blah...) { that was discussed in the other thread. Also, im going to be pushy, TURN ON ERROR REPORTING I have advised of this many times. PHP will TELL you what has happened if something has gone wrong, in this case you would have received an 'undefined variable' error. Put this at the top of ALL scripts whilst testing.. error_reporting(E_ALL); ini_set('display_errors',1); Edit: You could put it here foreach ($_SESSION['cart']['content'] as $content) { if ($content['size'] == "sizel") {$content['size'] = "Large";} echo "Product ID: {$content['id']}<BR>{$content['size']}<BR><BR>"; } But it will only be applicable inside that FOREACH loop. Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276309 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Author Share Posted October 6, 2011 I put it inside the section, but I still get the same result: if (isset($_GET['action']) && isset($_GET['size']) && $_GET['action'] == 'add') { $id = intval($_GET['id']); $size = $_GET['size']; $_SESSION['cart']['content'][] = array ('size' => $size, 'id' => $id); if ($content['size'] == "sizel") { $content['size'] = "Large"; } } Ahhh, that error reporting. I thought you meant the error reporting inside my web server error log. I placed that code in the document and its gave me the same error I see in my error.log file, so I will just refer to my error.log file. With the above code, I get this error: PHP Notice: Undefined variable: content on line 42 Which is: if ($content['size'] == "sizel") {$content['size'] = "Large";} Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276315 Share on other sites More sharing options...
trq Posted October 6, 2011 Share Posted October 6, 2011 There you go then. $content is not defined. Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276317 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Author Share Posted October 6, 2011 Edit: You could put it here foreach ($_SESSION['cart']['content'] as $content) { if ($content['size'] == "sizel") {$content['size'] = "Large";} echo "Product ID: {$content['id']}<BR>{$content['size']}<BR><BR>"; } But it will only be applicable inside that FOREACH loop. Ah, awesome man! Thats what I wanted, in each loop. Thanks! Why couldn't it go anywhere else in the document? Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276318 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 Should have been clearer.. The $content variable is ONLY defined inside the foreach (well it is after aswell but only the last iteration), so calling $content anywhere else isnt going to work. if (isset($_GET['action']) && isset($_GET['size']) && $_GET['action'] == 'add') { $id = intval($_GET['id']); $size = $_GET['size']; if ($size == "sizel") { $size = 'Large'; } $_SESSION['cart']['content'][] = array ('size' => $size, 'id' => $id); } Also, I question why you are passing sizel to the code when it is assigned to the size parameter? Couldnt you just pass size=Large in your url parameters? Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276320 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Author Share Posted October 6, 2011 Nah, because "sizel" is a dependency for another function I have with PHP & MySQL "sizel" is shared. http://i56.tinypic.com/ieed74.png Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276321 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 That fixes that then. Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276323 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Author Share Posted October 6, 2011 Thanks again man. Quote Link to comment https://forums.phpfreaks.com/topic/248524-if-not-working-but-syntax-correctly-used-must-be-something-else-what/#findComment-1276324 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.