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

Thanks, Dave.

That does work for hiding the notice. Much appreciated.

I'd still like to write the code so it just compares the items in an array to the value retrieved from the browsers navigation bar and returns only objects that are equal, but I haven't figured that out yet. (Probably easy for someone with more experience.) I'm thinking this should be done with a foreach loop, but haven't got that one down yet. For now, unless I discover it is fundamentally unsound, I am just going to go forward as is. What I've been able to achieve is satisfactory; albeit not ideal. I may revisit this down the road.

I've been using your CMS for a while now (love it!) and I'm just getting into writing and manipulating the PHP a little more than just implementing the generated code into my HTML. Kind of learning as I go (dangerous!) and I've hit a few walls along the way. This community here and a few others around the web have been great resources, as long as I know the right question to ask (or search.)

Josh

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.