• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In Model how to select all last data starting from 5th row ?

#1
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;
       
  }
Reply

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

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

#3
(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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.