Empty Record Errors

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 24, 2023   (RSS)

By rez - February 20, 2023 - edited: February 20, 2023

For a single record section, I just included an if statement in the body and removed the default "record not found" error in the list code. This allows pages to skip empty record sections without error. It works fine, however, in the past, there was no need for an if statement and no errors appeared. Was there a different default list code? I thought I used to simply add <?php echo $about_usRecord['content']; ?> wherever I needed it on a single record page.

Am I wrong? I guess I must be. It's odd that an error should be thrown from a CMS on a website because a record is empty (no content).

  list($about_usRecords, $about_usMetaData) = getRecords(array(
    'tableName'   => 'about_us',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $about_usRecord = @$about_usRecords[0]; // get first record


    <?php if($about_usRecord):?> // I don't recall this if check being required      
              <?php echo $about_usRecord['content']; ?>
    <?php endif?>

By rez - February 20, 2023 - edited: February 20, 2023

Actually, the code generator doesn't use if statements. My memory is bad, but I thought so!

OK, why am I getting this error everywhere for empty records then?

Notice: Trying to access array offset on value of type null in /home2/website/public_html/index.php on line 149

  list($about_usRecords, $about_usMetaData) = getRecords(array(
    'tableName'   => 'about_us',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $about_usRecord = @$about_usRecords[0]; // get first record



    <section class="theme-b">
      <div class="grid-container">		
        <div class="grid-x grid-padding-x text-center align-center-middle">
          <div class="cell medium-8 padding-vertical-2">
              <h2><?php echo $about_usRecord['title']; ?></h2>
              <?php echo $about_usRecord['content']; ?>
          </div>
        </div>
      </div>
    </section>

?

By Dave - February 24, 2023

Hi Rez, 

If there's no record then the $about_usRecord will be undefined, so you'll get errors.  

Normally, the code generator will display a die statement to check if the record isn't defined.

$about_usRecord = @$about_usRecords[0]; // get first record
if (!$about_usRecord ) { dieWith404("Record not found!"); } // show error message if no record found

So that's one option, or you can wrap the code you don't want displayed in an if block like you did, or you can add @ in front of the variables you don't want to display errors when they're undefined.

Hope that helps!  Let me know any questions.

Dave Edis - Senior Developer
interactivetools.com