I have “Undefined offset” error in Yii framework, I need go to first id if next id is null and if last id if previous id is null?
public static function getNextOrPrevId($currentId, $nextOrPrev)
{
$records=NULL;
if($nextOrPrev == "prev")
$order="id DESC";
if($nextOrPrev == "next")
$order="id ASC";
$records=Photo::model()->findAll(
array('select'=>'id', 'order'=>$order)
);
foreach($records as $i=>$r)
if($r->id == $currentId)
return $records[$i+1]->id ? $records[$i+1]->id : NULL;
return NULL;
}
error line
return $records[$i+1]->id ? $records[$i+1]->id : NULL;
But really, you should directly select the next id from the database, not select all ids and loop through them one by one in PHP.
(I don’t know Yii, so I’m just making the syntax up. You get the idea though.)