Search code examples
phpindexingundefinedisset

Undefined index: PHP Issue


Looks like this problem comes up alot, and I did some searching and based on that was going to put "isset" before $ _POST or $ _GET functions. Issue is in the code I have I can't figure out where to put it.

    foreach ($product['option'] as $option) {
        if ($option['type'] != 'file') {
            $value = $option['value'];
        } else {
            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

            if ($upload_info) {
                $value = $upload_info['name'];
            } else {
                $value = '';
            }
        }

the errors I get are the following:

Notice: Undefined index: option in /home/mdfehr/public_html/store/catalog/controller/common/cart.php on line 65Warning: Invalid argument supplied for foreach() in /home/mdfehr/public_html/store/catalog/controller/common/cart.php on line 65

Line 65 would be the first line in the code I posted. I have the same error in two other spots but I am hoping that what I learn from the suggestions here on this one can help me solve those.


Solution

  • Replace whole code like follows.

    if(isset($product['option']) && is_array($product['option']))
    {
        foreach ($product['option'] as $option) {
            if ($option['type'] != 'file') {
                $value = $option['value'];
            } else {
            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    
            if ($upload_info) {
                $value = $upload_info['name'];
            } else {
                $value = '';
            }
        }
    }