0

On my website I allow people to upload image galleries. When they click an image there is a next and previous button at the bottom so they can easily scroll back and forth through the images.

I am getting the below error in my log located at /opt/cpanel/ea-php72/root/usr/var/log/php-fpm/

NOTICE: PHP message: PHP Warning:  count(): Parameter must be an array or an object that implements Countable in . . . on line 12

It is talking about the line below in my code:

$max = count($photos);

Below is the other code that comes with that line:

$photos = get_field('gallery');
$max = count($photos);    <------- error line here -------->
$current = (isset($_GET['image'])) ? intval($_GET['image']) : false;
if ($current !== false) {
    if ($current > $max) $current = $max;
    if ($current < 1) $current = 1;
}

$next = (($current + 1) < $max) ? ($current + 1) : $max;
$prev = (($current - 1) > 1) ? ($current - 1) : 1;
?>

Basically this code uses get_field('gallery') to get the total amount of photos in the gallery and the number is assigned to variable max.

The rest of the code is how the next and previous buttons work.

I do not know what is wrong. Can somebody help with this?

4
  • What does get_field() do? Commented Jun 3, 2019 at 5:15
  • Hmmm let me see. It is part of the Wordpress theme files I am using so I don't quite know. Let me find out Commented Jun 3, 2019 at 5:18
  • 1
    Please do some basic debugging. Add a var_dump($photos); before your count($photos); to see what the variable actually contains. It's obviously not of any countable type. Also, get_field() is most commonly used with the plugin "Advanced Custom Fields". Commented Jun 3, 2019 at 5:18
  • Yes I see now. I have ACF installed. Thank you. Commented Jun 3, 2019 at 5:26

1 Answer 1

1

In the general, solution is simple:

First debbug with var_dump() what $photos return. Than you will know what's the issue.

count() accept array and if you have false, null or anything else you will have error.

Just do something like this:

$photos = get_field('gallery');
if(!is_array($photos) || empty($photos)) $photos = array(); // FIX ERROR!!!
$max = count($photos);    <------- error line here -------->
$current = (isset($_GET['image']) && !empty($_GET['image']) && is_numeric($_GET['image'])) ? intval($_GET['image']) : 0;
if ($current > 0) {
    if ($current > $max) $current = $max;
    if ($current < 1) $current = 1;
}

$next = (($current + 1) < $max) ? ($current + 1) : $max;
$prev = (($current - 1) > 1) ? ($current - 1) : 1;
?>

With if(!is_array($photos) || empty($photos)) $photos = array(); before $max = count($photos); you fixing your issue and anything what is not array or empty (0, NULL, FALSE, '') will be fixed and you will have in the count $max result 0 because array is empty.


IMPORTANT!!!

You should not work like this. You need to know what information you receive and expect in variables. The code must remain clean and correcting such errors is a bad practice. If you receive an array, then the array is expected and you must have checks before any calculating.


UPDATE:

Also you have error in

$current = (isset($_GET['image'])) ? intval($_GET['image']) : false;
if ($current !== false)

I fix it to work like this:

$current = (isset($_GET['image']) && !empty($_GET['image']) && is_numeric($_GET['image'])) ? intval($_GET['image']) : 0;
if ($current > 0)

Reason for that is that you have calculations below and you not expect (false + 1) to be something good. false can be translated to 0 but in your case you can also get error. For that case I replace false with 0, add empty() and is_numeric() check and you have no errors there.

Sign up to request clarification or add additional context in comments.

1 Comment

I will use this and see how it goes. Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.