문제

I have two variables, $take (limit) and $skip (offset) which are the values for a limit clause in mysql.

Now Code Igniter does it's limit clauses backwards.

E.g.

$this->db->limit(5,10) would product LIMIT 10, 5 in MYSQL.

Anyway, I am having a hard time getting my head around how to call the limit function based on which of the two values are set.

Basically,

If they are both set I want to call

$this->db->limit($take, $skip)

If only $take is set I want to call

$this->db->limit($take)

But what do I call if only $skip is set, e.g. if$skip = 10` then I want to show all rows, except for the first 10.

Do I call

$this->db->limit(null, $skip) or
$this->db->limit(0, $skip) or
$this->db->limit(false, $skip) or
something completely different?

도움이 되었습니까?

해결책

I think you found the answer you were looking for by answering our own question but if you want to use codeigniter to get all rows except for the first 10 then you would need to do something like (I have yet to test it):

$this->db->limit(18446744073709551615, 10)

According to Mysql Documentation

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter.

다른 팁

Turns out Code Igniter has an undocumented offset function.

/**
* Sets the OFFSET value
*
* @param    integer the offset value
* @return   object
*/
public function offset($offset)
{
    $this->ar_offset = $offset;
    return $this;
}

So just calling $this->db->offset($skip) works fine!

This is the official documentation to offset function in active record of codeigniter 3

offset($offset) Parameters: $offset (int) – Number of rows to skip Returns:
CI_DB_query_builder instance (method chaining)

Return type:
CI_DB_query_builder

Adds an OFFSET clause to a query.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top