Notice: Undefined offset: 1. How fix so it ignores empty arrays or write differently?

6 posts by 2 authors in: Forums > CMS Builder
Last Post: December 27, 2013   (RSS)

Hello, I am having trouble with a bit of code. I am trying to write it so that $record['medium:labels'] is compared to $tagLink and it will display an image is the two are equal. The bit of code below does return the proper images, but I am also getting a "Notice: Undefined offset: 1" on line 126 (marked below and see attached image). I believe this is due to the fact that not all of the uploads have a second value in the array, but I'm not sure if that is the cause.

Is there a way to fix this so it ignores the empty array, if that is the case? Or, I feel that there may be a better way to write this code so it compares $record['medium:labels'] and $tagLink and only returns what is equal without calling out each key. (That would be preferable, so I don't have to limit the user on how many selections they may make.)

Any help would be greatly appreciated.

~Josh


<?php foreach ($imagesRecords as $record): ?>
     <?php foreach ($record['thumbnail'] as $index => $upload): ?>
          <?php $medium = $record['medium:labels'] ?>
          <?php $tagLink = $_GET['tag'] ?>
               <?php if ($medium[0] == $tagLink) : ?>
                    <!-- do some HTML that generates image -->
               <?php elseif ($medium[1] == $tagLink) : ?>    <!-- This is Line 126 -->
                     <!-- do some HTML that generates image -->
               <?php endif
    <?php endforeach ?>
<?php endforeach ?>

Attachments:

screenshot_002.jpg 407K

By Dave - December 18, 2013

Hi Josh, 

PHP has an "Error Control Operator" of @ that suppresses errors or warnings from whatever comes immediately after it.

So try adding a @ in front of whatever is generating the error, such as: @$medium[1]

Hope that helps!  Let me know if that works for you.

 Reference: http://php.net/manual/en/language.operators.errorcontrol.php

Dave Edis - Senior Developer
interactivetools.com

By Dave - December 18, 2013

Hi Josh, 

Thanks for clarifying your question and glad you're enjoying CMSB! 

What about something like this (untested code): 

<?php foreach ($imagesRecords as $record): ?>
     <?php foreach ($record['thumbnail'] as $index => $upload): ?>
          <?php foreach ($record['medium:labels'] as $label): ?>
               <?php if ($label != @$_GET['tag']) { continue; } // skip labels that don't match ?>
               <!-- do some HTML that generates image -->
          <?php endforeach ?>
     <?php endforeach ?>
<?php endforeach ?>

I think that should work, but it seems like it's looping more than needed.  Another option might be to use the built in search features.  See: http://www.interactivetools.com/docs/cmsbuilder/viewer_search.html

Just make sure you have 'allowSearch' => true, (or just not set to false) in your getRecords() call, and then pass your tag as ?medium=value or ?medium_keyword=value instead of ?tag=value

Then you could just use this code

<?php foreach ($imagesRecords as $record): ?>
     <?php foreach ($record['thumbnail'] as $index => $upload): ?>
          <!-- do some HTML that generates image -->
     <?php endforeach ?>
<?php endforeach ?>

Let me know if one of those solutions work for you or if you have any questions or further issues.  Cheers!

Dave Edis - Senior Developer
interactivetools.com

Hi Dave,

I used the first set of code and it works excellent. This is exactly what I was trying to achieve and now that I see it it makes perfect sense. Thank you very much.

As far as your second set of code: I have not used the CMS's search feature before, but now that I understand that it can achieve this I will certainly look for it in the future. (For reasons of adjusting code that is related to the links on other pages and time, I'm not going to implement it in this site at this point.)

Happy Holidays and the Best for a New Year!

Josh

I just used the second set of code on another site. So simple. Learning something new every day with PHP. Thanks again.