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';
}
else{
$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.
D
Edit: Updated to include link to full PHP code onlinephp.io