从记录集中获取下一行和上一行

问题描述:

我正在尝试使用Codeigniter的活动记录来检索数据库中的下一行。

I am trying to retrieve the next row in my database using Codeigniter's active record.

模型

public function getNextRow()
{
    $this->db->where('id', 1);
    $this->db->limit(1);
    $query = $this->db->get('portfolio_shots');
    return $query->next_row();
}

控制器

public function test()
{
    $nxt = $this->Portfolio_model->getNextRow();
    echo $nxt->id;
}

输出

为什么会出现错误


试图获取非对象的属性'id'

"Trying to get property 'id' of non-object"

next_row() 返回:


结果集的下一行;如果没有,则为NULL不存在:

Next row of result set, or NULL if it doesn’t exist:

因此,如果没有下一个结果集,则会出现错误

so, if there is no next result set, you get the error


试图获取非对象的属性

"Trying to get property of non-object"

因为 $ nxt 为null而不是结果集,因此您不能 $ nxt-> i d;

because $nxt is null and not a result set and therefore you cannot $nxt->id;

您会遇到这种情况,因为您的查询只将结果集限制为1行 (因此不再有行)

you get into this situation, because your result set is limited by your query to 1 row only (therefore no more rows)

无论如何,您都可以在模型中处理此问题(删除限制):

anyway you can handle this in your model (removing limits):

public function getNextRow()
{
    $query = $this->db->get('portfolio_shots');
    return $query->next_row();
}

在控制器中(如果到达最后一行,则处理该情况):

and in the controller (handle case if last row was reached):

public function test()
{
    $nxt = $this->Portfolio_model->getNextRow();
    if($nxt){
      $nxt->id;
    }else{
      echo 'EOF';
    }
}