Getting last record php warning

4 posts by 3 authors in: Forums > CMS Builder
Last Post: June 22, 2020   (RSS)

By hiroko - June 20, 2020

Hi,

I am trying to use end($myRecords) to get the 'num' of the last record listed in a viewer.

The result is fine, but I have a PHP warning that says 

"Warning: Use of undefined constant     - assumed '   ' (this will throw an Error in a future version of PHP)"

I just want to get rid of the comma on the last item.

<?php // get last record num
	$lastRecord = end($itemRecords);
	$lastRecordNum = $lastRecord['num'];
?>
<?php foreach ($itemRecords as $record): ?>
some codes
<?php if ($lastRecordNum != $record['num']): ?>,<?php endif ?>
<?php endforeach ?>

Can someone tell me why I am getting this warning and how I could fix it?

Thank you,

Hiroko

By daniel - June 22, 2020

Hi Hiroko,

Generally, your code should work; the error message indicates that a "bad" whitespace character has been copied into your code. Essentially an invisible character that looks like a space or tab but isn't recognized as such by the PHP engine. One way to find the offending character is if your code editor has an option along the lines of "show hidden/invisible/non-printable characters" - with this turned it should stand out from the rest of the whitespace in your code, allowing you to delete/replace it. If you're unable to do this, I would recommend commenting/removing portions of your code until you find the line(s) that trigger the error, then re-typing that code from scratch.

Also, in addition to Jerry's method, another potentially simpler way to handle the "," could be to use a counter variable, something like this:

<?php $count = 0; ?>
<?php foreach ($itemRecords as $record): ?>
  <?php if ($count++ > 0): ?>,<?php endif; ?>
  some codes
<?php endforeach ?>

Let me know if you have any more questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By hiroko - June 22, 2020

Hi,

Thanks for the help.

I tried following Jerry's advice but couldn't figure out how to apply it to record view, not images, and have been working around, but suddenly, the error stopped when I typed in the first code again.

I guess I had something wrong somewhere.

Sorry for the fuss and thank you!

Best,

Hiroko