as you are all probably aware PHP is not my expertise. I'm helping plan a Joomla migration to PHP 8 (J3) and a previous developer has created a com_search override of the default_results.php file.

The 2 PHP notices I get in PHP 7.4 are:

Notice: Undefined offset: 0 in /public_html/templates/{template}/html/com_search/search/default_results.php on line 27

Notice: Trying to get property 'id' of non-object in /public_html/templates/{template}/html/com_search/search/default_results.php on line 138

The first notice refers to the return $items[0]; in the code below:

function paginateTable($tableData = null, $limit1, $limitStart1){
    if(false === is_null($tableData)){
        $items = array_chunk($tableData, $limit1);
        $posToShow = 0;
        if(sizeof($tableData) > $limit1){
            for($i = 0; $i < sizeof($items); $i++){
                $posToShow = $limitStart1/$limit1;
                if($limitStart1 > 0){
                    return $items[$posToShow];
        return $items[0];
    }//end is_null($tableData)

The second notice refers to $Itemid = $menuItem->id; in the code below:

foreach($all_results as $result)
    if($result->catid == 18)
        //$result->href = 'index.php?option=com_news&view=article&id=' . $result->id . '&Itemid=119';
        $result->href = 'index.php/news/' . $result->id . '-'.$result->alias;
        $result->description = html_entity_decode($result->description);
        $result->category = 'News';
    }elseif($result->catid == NULL){
        $result->rp_id = $result->link_id;
        $result->alias = strtolower(str_replace('\'','-', $result->title)); 
        $result->alias = str_replace(' ','-', $result->alias);
        $result->href = '/index.php/events-calendar/eventdetail/'.$result->rp_id.'/-/'.$result->alias;
        $result->description = html_entity_decode($result->description);
        $result->category = 'Events';
        $link = 'index.php?option=com_content&view=article&id='.$result->id;  
        $menuItem = $menu->getItems( 'link', $link, true );
        $Itemid = $menuItem->id;
        $count_link = "index.php?option=com_content&view=article&id=" . $result->id . "&Itemid=" . $Itemid;     
        $result->href = $count_link;
        $result->description = html_entity_decode($result->description);    
        $result->category = 'Web Pages';

I was wondering if there is a way to make this code PHP 8 compatible - there obviously may be more errors on fixing these. I'm happy to provide the entire file if needed. When switching to PHP 8 the page displays:

0 - sizeof(): Argument #1 ($value) must be of type Countable|array, null given

Which I assume is because the array returs empty. I vaguely remember something about changing from square brackets for a similar problem but I'd like a second opinion if possible.

As always, thank you for your time. It's really appreciated.


Edit: Updated to include link to full PHP code onlinephp.io

  • 1
    Is there a reason that you cannot simplify the pagination process to be something similar to this? 3v4l.org/P545l Commented Mar 26, 2023 at 20:47
  • @mickmackusa As always you are my hero. I'm not a PHP guru so as the inheritor of the override it felt like a wood/trees situation. This solution allows the override to work on PHP 8. Please add it as an answer. There is a PHP warning regarding line 111 which i'd like to query on your answer if that's ok?
    – Dtorr1981
    Commented Mar 26, 2023 at 23:20
  • 1
    Well, I'm no Joomla guru, so hopefully this community can find a way to solve your problems. I don't see anything overtly wrong with line 111. You'll need to be more specific with what is going wrong. Commented Mar 26, 2023 at 23:35
  • @mickmackusa The search override is working, that's the main thing. Thank you so much. The PHP warning isn't a huge priority as the client may be persuaded to upgrade/migrate in the near future. Again, thank you for your expertise.
    – Dtorr1981
    Commented Mar 27, 2023 at 1:11

2 Answers 2


Without understanding much of the context of the task, I can advise a simpler pagination method.

My opinion is that it is almost never appropriate for an array parameter to be null. In my applications, if I am logically expecting a variable to contain iterable data, I do not allow any scalar or null values.

My snippet assumes that your starting integer is zero-based (not one-based). I am building in a protection from negative starts and negative lengths using abs() because array_slice()'s behaviour with negative values may not be what you want. (If you understand the consequences of negative integers with this function and actually desire the behaviour, then the abs() calls can be removed.

Code: (Demo)

function paginateTable(array $data = [], int $limit = 10, int $start = 0): array
    return array_slice($data, abs($start), abs($limit));

As for the problem with $menu->getItems( 'link', $link, true );, according the Joomla documentation, you can most directly call something like:

$menuItem = $menu->getItem($result->id);

...if I am understanding the context/data.


for your first notice, array_chunk either returns an array or false, but your return statement always assumes that it's going to be an array. To fix this immediate issue, I would replace your return statement with a ternary check:

return $items && isset($items[0]) ? $items[0] : null;

This way, it checks that the zero indexed array item exists before returning it, otherwise it returns null. You'll need to check that returning null doesn't cause an issue in whichever function is calling it. It may be that returning false may be the required option but I'd need to see the code to work that out.

For your second notice, the problem is that $menu->getItems looks to be assuming that the result is going to be a single menu object, but getItems should be returning an array of those objects. If you look at this info section it shows you how you need to loop through the result of getItems to access the menu objects your code is expecting - https://docs.joomla.org/Menu_and_Menuitems_API_Guide#Properties_and_Parameters

Hope that gets you moving in the right direction but post back if you're still stuck.

  • 1
    array_chunk()'s only return type is array. php.net/manual/en/function.array-chunk.php The function will never return false. Menu items can be fetched directly by their id (no iterating is needed). Commented Mar 26, 2023 at 20:20
  • Thanks Mick. I must have mis-read the docs about the false return value
    – Grant Hood
    Commented Mar 27, 2023 at 13:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.