0

I'm really stumped on this one. I have this array variable $banner_image_description. When I print it I get Array ( ['title'] => dafha ['price'] => adhfadhf ).

Now, what I'm trying to do is check the length of the value at 'title' and make sure it is more than 2. So when I run :

if (isset($this->request->post['banner_image'])) 
    {
        foreach ($this->request->post['banner_image'] as $banner_image_id => $banner_image) 
        {
            foreach ($banner_image['banner_image_description'] as $language_id => $banner_image_description) 
            {   
                print_r($banner_image_description);
                echo('<br/>');
                if ((utf8_strlen($banner_image_description['title']) < 2) || (utf8_strlen($banner_image_description['title']) > 64)) 
                { 

                    $this->error['banner_image'][$banner_image_id][$language_id] = $this->language->get('error_title'); 
                }                   
            }
        }   
    }

I get Notice: Undefined index: title and I have no idea why. I thought this was the correct way to go about getting the value at 'title' and I know the index is there and the value is there.

I am pretty sure that the array is being populated by POSTing form values:

if (isset($this->request->post['banner_image'])) 
{
    $banner_images = $this->request->post['banner_image'];
}

Here is what the form values look like:

<input type="text" 
 name="banner_image[<?php echo $image_row; ?>][banner_image_description][<?php echo $language['language_id']; ?>]['title']" 
 value="<?php echo isset(
 $banner_image['banner_image_description'][$language['language_id']]['title']) ? 
 $banner_image['banner_image_description'][$language['language_id']]['title'] : ''; ?>" />
 <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /><br />
6
  • Is it like Array ( ['title'] => dafha ['price'] => adhfadhf ) or Array ( ['title'] => 'dafha', ['price'] => 'adhfadhf' )? Commented Feb 15, 2013 at 22:45
  • BUt is it the way you define it or the result of print_r($var)? Because if you are defining it, should be the way I said the 2nd. Commented Feb 15, 2013 at 22:48
  • Can you also show us how you define $banner_image_description ? Commented Feb 15, 2013 at 22:49
  • It's the result of print_r($banner_image_description); Commented Feb 15, 2013 at 22:49
  • Sounds like you are accidentally overwriting it somewhere; this is the correct way and if the variable is what you say it is the line before your if, it should work / not generate that error message. Or a scope problem, could you post the complete relevant code? Commented Feb 15, 2013 at 22:50

2 Answers 2

1

That is a warning, it is poor programming but will not break anything yet.

Do this, it will make sure the key is set and has a value. Keep in mind if this is not set so the key has no value, your echo statement will not trigger.

if (isset($banner_image_description['title']) && (utf8_strlen($banner_image_description['title']) < 2)) 
{ 
    echo 'too short!';
}
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't answer anything. The fact that it is throwing the error means that the key is not set. Yet when I print it shows that the key is set.
Considering you did not even post how the array was populated, I had to assume. Please post how this array is being populated and I may be able to help you further. Your lack of programming abilities is why this is coming up. You are nesting foreach's within each other. Doing this effects performance and makes your code sloppy. Rewrite it. Also I will bet you that by implementing my above code, it will fix the error.
As I said in previous comments the code pre written in the OpenCart framework and since the framework spans dozens of files I'd rather not rewrite half the framework. I will update my question with where I think it is being initialized.
0

var_dump() revealed that the key is including ' So the if statement should be set to

if ((utf8_strlen($banner_image_description["'title'"]) < 2) || (utf8_strlen($banner_image_description["'title'"]) > 64))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.