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)

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

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.