jarvis Posted April 8, 2022 Share Posted April 8, 2022 Good morning, I've the following code: $message = ''; if ('delete' === $table->current_action()) { $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>'; } Which seems to cause a warning of: Quote Warning: count(): Parameter must be an array or an object that implements Countable in When I've had similar before, it's been a case of doing something like changing: if (count($_REQUEST['id']) > 0) : To: if (is_countable($_REQUEST['id']) && count($_REQUEST['id']) > 0) : But I can't fathom out how to apply the same principal to the above code. Or have I misunderstood the warning? Quote Link to comment https://forums.phpfreaks.com/topic/314682-warning-count-parameter-must-be-an-array-or-an-object/ Share on other sites More sharing options...
jarvis Posted April 8, 2022 Author Share Posted April 8, 2022 D'oh! Solved it by casting it to an array: count((array)$_REQUEST['id'])) Quote Link to comment https://forums.phpfreaks.com/topic/314682-warning-count-parameter-must-be-an-array-or-an-object/#findComment-1595149 Share on other sites More sharing options...
mac_gyver Posted April 8, 2022 Share Posted April 8, 2022 all that did was hide the actual problem. if your code is expecting an array of data as its input, it's an application error if it isn't. either a programming mistake or an incorrect user action (directly requesting a delete action page without first visiting the page selecting what to delete) caused the expected input to not be the expected data type. for programming mistakes, you need to find an fix the root cause of the problem. for an incorrect user action, you need to setup and display a message telling the user either what they did wrong, i.e. you cannot directly request this page, or what they need to do to correct the problem, i.e. you must select at least one notification to delete. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314682-warning-count-parameter-must-be-an-array-or-an-object/#findComment-1595150 Share on other sites More sharing options...
jarvis Posted April 8, 2022 Author Share Posted April 8, 2022 Oh dear 😢 That's certainly popped by bubble. I genuinely thought I'd nailed it. I best go revisit the code Thanks @mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/314682-warning-count-parameter-must-be-an-array-or-an-object/#findComment-1595152 Share on other sites More sharing options...
gizmola Posted April 9, 2022 Share Posted April 9, 2022 If you step back from the minutae for a second and look at what your code is doing: if ('delete' === $table->current_action()) { $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>'; } This has an implicit assumption built into it: so long as the current_action() result is the string 'delete' then you assume that there was notifications deleted. What if there were no notifications deleted, or the list to be deleted was empty? So how could you fairly easily fix this issue, where you need to be sure that there is a reason to display this message, while also providing additional feedback to the UI? if ('delete' === $table->current_action()) { if (!empty($_REQUEST['id']) && is_array($_REQUEST['id'])) { $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>'; } else { $message = '<p>Nothing to delete.</p>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/314682-warning-count-parameter-must-be-an-array-or-an-object/#findComment-1595198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.