E_NOTICE: Trying to access array offset on value of type null?

8 posts by 2 authors in: Forums > CMS Builder
Last Post: September 21, 2022   (RSS)

By daniel - September 21, 2022

Hey Jerry,

In this case, putting an @ in an if condition - when you're just checking for the existence of a value - is generally "safe" though it's no longer considered best practice. A fairly simple and better practice method to avoid the notice is with the empty() function, which checks "does this variable exist and contain a nonzero value?" So instead of this:

<?php if (@$searchField['requiresLabel']): ?>

You would use:

<?php if (!empty($searchField['requiresLabel'])): ?>

Let me know if that helps!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 21, 2022

Hey Daniel,

Glad I asked. 

I'll give it a  try.

Any idea why this is happening in the first place?

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 gkornbluth - September 21, 2022 - edited: September 21, 2022

Thanks Daniel,

No errors now...

Just curious if I did the right thing, Daniel.

One of the offending lines was:

<?php echo $searchField['html'] ?>

So I surrounded it with an if to check if the value was empty,

<?php if (!empty($searchField['html'])): // added 9/12/22 ?>
<?php echo $searchField['html'] ?>
<?php endif ?>

I'm still interested to find out why I got these errors in the first place.

At any rate, here's what I did in case someone else come up with the same kind of issue. (starts at line 113 in version 3.56 in cmsAdmin/lib/menus/default/list.php)

<!-- simple search -->
<?php $searchField = getSearchField($primarySearchRow, 'PRIMARY'); ?>
<?php // if ($searchField['requiresLabel']): ?>
<?php if (!empty($searchField['requiresLabel'])): // added 9/12/22 per Daniel ?>
<div class="row" style="margin-bottom: 5px;">
<div class="col-md-2">
<label class="control-label">
<?php echo $searchField['label'] ?>
</label>
</div>
<div class="col-md-10">
<?php echo $searchField['html'] ?>
</div>
</div>
<?php else: ?>
<div class="row" style="margin-bottom: 5px;">
<div class="col-md-12">
<?php if (!empty($searchField['html'])): // added 9/12/22 ?>
<?php echo $searchField['html'] ?>
<?php endif ?>
</div>
<?php // if ($searchField['description']): ?>
<?php if (!empty($searchField['ddescription'])): // added 9/12/22 per Daniel ?>
<div class="help-block col-md-12">
<?php echo $searchField['description']; ?>
</div>
<?php endif; ?>
</div>
<?php endif ?>

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 daniel - September 21, 2022

Hey Jerry,

Apologies, I didn't realize at first this was happening on a CMSB admin page!

It looks like this error shows up if a section isn't configured with any search fields in the section editor (under the "Sorting" tab when editing a section). If you add any search field (or just "_all_"  to restore the default search bar) it should get rid of the errors.

From what I can see, your changes are probably sufficient, but here's a more "official" patch that should fix the errors in that section:

      <!-- simple search -->
      <?php $searchField = getSearchField($primarySearchRow, 'PRIMARY'); ?>
      <?php if (!empty($searchField)): ?>
        <?php if ($searchField['requiresLabel']): ?>
          <div class="row" style="margin-bottom: 5px;">
            <div class="col-md-2">
              <label class="control-label">
                <?php echo $searchField['label'] ?>
              </label>
            </div>
            <div class="col-md-10">
              <?php echo $searchField['html'] ?>
            </div>
          </div>
        <?php else: ?>
          <div class="row" style="margin-bottom: 5px;">
            <div class="col-md-12">
              <?php echo $searchField['html'] ?>
            </div>
            <?php if ($searchField['description']): ?>
              <div class="help-block col-md-12">
                <?php echo $searchField['description']; ?>
              </div>
            <?php endif; ?>
          </div>
        <?php endif ?>
      <?php endif; ?>

This would replace lines 114-137 in /cmsb/lib/menus/list.php (the original version, before your changes)

Let me know if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 21, 2022 - edited: September 21, 2022

Thanks Daniel,

Appreciate your effort. I'll apply that solution

Is that going to be implemented going forward?

There's an instruction that comes up in the editor when you go to enter the search criteria that says:

Disabling Search If you don't want any search options displayed just leave the box above blank.

This seems contrary to the code that was there in lists.php

Jerry

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 daniel - September 21, 2022

Hey Jerry,

Yes - I'll get that patch in the next CMSB release so you don't have to worry about it being overwritten in future updates.

I believe this specific error is new in PHP 7.4, so it likely worked to disable the search fields in this way when it was written. But it's not something we see commonly used, so the conditions for the error are pretty rare.

In any case, thanks for reporting this!

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 21, 2022

Glad to help

Have a great rest of the week (and beyond...)

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