suppressing foreach loop erros on detail pages

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

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

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

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

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

Hi all,

Jenna's suggestion was very helpful, and solved the error log issue, but shows a blank white page with the words "Record not found!".

Since there's a slim possibility that a live visitor is receiving this message and not a bot, I used a variation on the theme that redirects the visitor to a custom 404 page instead if a record is not found, using this code: (You'll have to create your own appropriate page for this purpose)


<?php if (!$your_sectionRecord) { header('Location: http://www.your_site.com/your_new_page.php'); die;  } ?>


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. 

When I encounter pages that throw a 'header' error, I find it easier to find a similar detail page and copy the load viewer library and get records calls to the top of the faulty page. That usually solves the problem.

Hope that helps,

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

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