Limit Method

The limit method is part of Query Builder for limiting query result by executing LIMIT [A],[B] SQL command. This method also suitable for creating pagination with datatables pagination.

Basic Usage

Here is the basic usage limit method from $db property on SENE_Model class.

$this->db->limit(int $offset, int $count): $this->db

Parameters

This method has 2 required parameters.

$offset

The $offset value can be zero or positive integer for specifying the offset of the first row to be returned.

$count

The $count value can be zero or positive integer for specifying the maximum number of rows to be returned.

Example

On this example will show limiting the result query by using limit method in a model class.

<?php
class D_Blog_Model extends SENE_Model{
  var $tbl = 'd_blog';
  var $tbl_as = 'b';
  public function __construct(){
    parent::__construct();
  }
  public function latest(){
    $this->db->select("*");
    $this->db->from($this->tbl,$this->tbl_as);
    $this->db->order_by("date_create","desc");
    $this->db->limit(0,5);
    return $this->db->get();
  }
  public function latest3ExceptTheFirst(){
    $this->db->select("*");
    $this->db->from($this->tbl,$this->tbl_as);
    $this->db->order_by("date_create","desc");
    $this->db->limit(1,4);
    return $this->db->get();
  }
}

SQL Result

The following is the SQL command that generated from D_Blog_Model model methods.

-- result from executing D_Blog_Model::latest() --
SELECT *
FROM `d_blog` b
ORDER BY `date_create` DESC
LIMIT 0, 5;
-- result from executing D_Blog_Model::latest3ExceptTheFirst() --
SELECT *
FROM `d_blog` b
ORDER BY `date_create` DESC
LIMIT 1, 4;

Page method v.s. Limit Method

Page method used for limiting by page and page size.

Limit method used for limiting data by MySQL traditional limit method.