Get next and previous row from record set

后端 未结 1 1671
天涯浪人
天涯浪人 2021-01-23 16:43

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

Model

public function getNextRow()
{
    $this-&         


        
相关标签:
1条回答
  • 2021-01-23 16:59

    next_row() returns:

    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"

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

    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';
        }
    }
    
    0 讨论(0)
提交回复
热议问题