suppressing foreach loop erros on detail pages

9 posts by 2 authors in: Forums > CMS Builder
Last Post: February 10, 2021   (RSS)

By gkornbluth - January 19, 2021

Hi all,

I’ve got a detail page that has a foreach loop for an image upload on line 71 with the following code:

<?php foreach (@$musicians_lisingsRecord['list_page_image'] as $index => $upload): ?>
<img src="<?php echo htmlencode(@$upload['thumbUrlPath3']) ?>" width="<?php echo @$upload['thumbWidth3'] ?>" height="<?php echo @$upload['thumbHeight3'] ?>" alt="" />
<?php endforeach ?>

(The @ were added to attempt suppressing errors)

When a bot accesses the detail page without an appended record number, I get the following error added to the error log:

E_WARNING: Invalid argument supplied for foreach()
/home4/zcfzmsmy/public_html/jazzonjstreet/musicians_detail.php (line 71)
https://jazzonjstreet.com/musicians_detail.php

Any thoughts on how to suppress this type of error so it doesn’t create a pile of meaningless entries in the error log?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Jenna - January 26, 2021

Hi Jerry,

Have you tried adding an if statement outside of the foreach loop you are doing, to check if the $musicians_lisingsRecord['list_page_image'] exists?

<?php if ($musicians_lisingsRecord['list_page_image']) : ?>
  <?php foreach ($musicians_lisingsRecord['list_page_image'] as $index => $upload): ?>
    <img src="<?php echo htmlencode($upload['thumbUrlPath3']) ?>" width="<?php echo $upload['thumbWidth3'] ?>" height="<?php echo $upload['thumbHeight3'] ?>" alt="" />
  <?php endforeach ?>
<?php endif ?>

Please let me know if this helps you at all.

Jenna Cooke - PHP Programmer
interactivetools.com

By gkornbluth - January 26, 2021

Thanks Jenna

I'll give it a try tomorrow morning and let you know.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - January 26, 2021

Sorry Jenna, but I still get an error:

E_NOTICE: Trying to access array offset on value of type null
/home4/zcfzmsmy/public_html/jazzonjstreet/musicians_detail.php (line 71)
https://jazzonjstreet.com/musicians_detail.php

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Jenna - January 28, 2021

Hi Jerry,

Just so I'm clear on this, the error is because there is no $musicians_lisingsRecord at all because there's no parameter passed at the end of the url?

My guess would be that you need to check if $musicians_lisingsRecord is set first then.

<?php if (isset($musicians_lisingsRecord) && $musicians_lisingsRecord['list_page_image']) : // Because $musicians_lisingsRecord will not be set if no url param passed?>
  <?php foreach ($musicians_lisingsRecord['list_page_image'] as $index => $upload): ?>
    <img src="<?php echo htmlencode($upload['thumbUrlPath3']) ?>" width="<?php echo $upload['thumbWidth3'] ?>" height="<?php echo $upload['thumbHeight3'] ?>" alt="" />
  <?php endforeach ?>
<?php endif ?>

Or you could try near the top of your file, where you first getRecords for $musicians_lisingsRecord, the following:

if (!$musicians_lisingsRecord) { dieWith404("Record not found!"); }

Please let me know if this helps at all.

Jenna Cooke - PHP Programmer
interactivetools.com

By gkornbluth - January 28, 2021

Good catch, Jenna,

Adding to the if statement  worked like a charm

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Jenna - February 10, 2021

Hi Jerry,

Just wanted to add something to this thread. Built into CMSB is a function called redirectBrowserToUrl (defined in lib/http_functions.php) which essentially provides the functionality you mentioned here, without having to write the header('Location: http://fullurl.com') and the die().

It can be used like so:

if (!$your_sectionRecord) { redirectBrowserToUrl('your-new-page.php'); }

Note that since this still uses the header('Location...') functionality of php it still needs to be be put before any other kind of information, as you mentioned in your post (copied below).

Just remember that since you're setting an HTTP-header, you must not have sent any kind of output before (not even a blank space at the end of an included file) or you'll throw a "Warning: Cannot modify header information - headers already sent" error. Test your page after the modification to make sure that it works as planned, and proceed accordingly. 

Please let me know if this is useful for you.

Jenna Cooke - PHP Programmer
interactivetools.com

By gkornbluth - February 10, 2021

Thanks Jenna,

Don't know how I would have known that without you mentioning it.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php