Jump to content

Undefined Index Error


Archemedees

Recommended Posts

I realize this is a very common issue and it stems from at least the way I understand it, the code not checking if something is declared before declaring it. That begin said I tried adding isset code to some sections where I was getting errors and they are still errors there so Im totally lost. I am very new to php so if this is a stupid question please treat me like a 5 year old if you would and help me understand my errors :)

 

The errors are as follows:

 

 

Notice: Undefined index: key in /home/mdfehr/public_html/store/catalog/controller/common/cart.php on line 103Notice: Undefined index: recurring in/home/mdfehr/public_html/store/catalog/controller/common/cart.php on line 108

 

And the Code it pertains to(starts at line 103): 

			$data['products'][] = array(
				'key'       => $product['key'],
				'thumb'     => $image,
				'name'      => $product['name'],
				'model'     => $product['model'],
				'option'    => $option_data,
				'recurring' => ($product['recurring'] ? $product['recurring']['name'] : ''),
				'quantity'  => $product['quantity'],
				'price'     => $price,
				'total'     => $total,
				'href'      => $this->url->link('product/product', 'product_id=' . $product['product_id'])
			);
		}

Any suggestions are appreciated. 

Link to comment
Share on other sites

the error means an array element is being referenced that doesn't exist.

 

adding isset() to test if something exists before trying to referenced it is only valid if what is being referenced is optional, i.e. it may or may not be present. this would be the case when testing if a page got requested and a form may or may not have been submitted, a form element may or may not exist (checkboxes, radio buttons), a get parameter in the url may or may not be present, or code didn't find any matching row in a database table and the code didn't take the added step of preventing any follow on code that's dependent on that data from running.

 

for data that the code requires to be present, you would not use isset() to prevent the error. you would find out why the expected data isn't present by tracing back in the code to find out where it is being produced at and find out why it isn't being produced. adding isset() in this case would just be hiding problems.

 

for the missing $product['key'], i would expect that the key element is required for this code to work at all. if some of the other $product[...] elements are present, but not the ['key'], you would need to find out why. one possible cause would be a difference in the index name from where the value is being produced and where it is being used. it might be ['id'] or ['product_id'] rather than ['key'].

 

for the missing $product['recurring'], that one sounds like it could be optional. but it depends on where and how it is being stored/produced to determine if using isset() to test if it exists before referencing it would be appropriate.  if the line of code for it in what you posted is the way the original code is, it would be a safe guess that it is missing an isset() around the $product['recurring'] in that line.

 

sadly, problems like this in code you find posted on the web are due to the person writing the code not really knowing the reason behind what they are doing and are developing code with php's error reporting not fully turned on and/or not fully testing the code they have posted.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.