CodeIgniter Forums
In Model how to select all last data starting from 5th row ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: In Model how to select all last data starting from 5th row ? (/showthread.php?tid=450)



In Model how to select all last data starting from 5th row ? - paju89 - 12-06-2014

For examle there is 10 rows in table. My issue is how to fetch all data but starting from 5th row and 4 3 2 1 and so on?
Below code works but only with id numbers. so when my datas in table id will be mixed up like 50 46 42 40 and so on that code wont work.

Code:
public function fetch_data() {
         
       $maxid=5;
       
     
       $this->db->where('site_id <',$maxid);
       $this->db->order_by('site_id', 'DESC');
       $query = $this->db->get("sites");

       if ($query->num_rows() > 0) {
           foreach ($query->result() as $row) {
               $data[] = $row;
           }
           return $data;
       }
       return false;
       
  }



RE: In Model how to select all last data starting from 5th row ? - includebeer - 12-07-2014

Remove the "where" clause and use "limit" :

$this->db->limit(5);


RE: In Model how to select all last data starting from 5th row ? - paju89 - 12-08-2014

(12-07-2014, 07:11 PM)includebeer Wrote: Remove the "where" clause and use "limit" :

$this->db->limit(5);
thank you for reply
if if use limit(5) result will give me last 5 rows but my question is quite different. and yesterday i found solution for that

added following code in
Code:
$q = $this->db->query('SELECT site_id FROM sites ORDER BY site_id DESC LIMIT 1');

$row = $q->row();
$b = $row->site_id+1-5;
$this->db->where('site_id <',$b);


and worked like a charm