suppressing foreach loop erros on detail pages

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

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

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

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