Jump to content

if() not working but syntax correctly used, must be something else.. what?!


Recommended Posts

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?

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.

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";}

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.