Welcome Guest, Not a member yet? Register   Sign In
How to get max id value?
#1

[eluser]Unknown[/eluser]
Actualy in my code i get max id value but i need everytime when i do request get second max id value. and after that when i do request it retrives me 3rd max id.

here is mysql table


id name
1 first

2 second

3 example

15 merlin

i have button when i press it it only shows me last id=15 data and when i second time press it i want it shows me id=3 data and third time id = 2.

here is my code

Code:
public function getSites() {
      
                 $this->db->select('*');
                
                 $this->db->order_by('site_id', 'DESC');
                 $this->db->limit(1);
                 $query = $this->db->get('sites');
                
              return   $query->result_array();

}


if you are aware of how stumbleupon works you understand concept of my problem. one button and everytime you click it it takes you to some page.

#2

[eluser]Flemming[/eluser]
You need to pass the previous max to the function each time - you could do this either by changing the URL, e.g.

/yoursite.com/controller/method/3 (where 3 is the max ID you just got from your query)

or you could store the last max ID in a session

either way, you then need to update your query so that it gets theID which is LESS than the previous ID:

Code:
public function getSites($last_id = null) {

$this->db->select('*');
if($last_id)
{
  $this->db->where('site_id <', $last_id);
}
$this->db->order_by('site_id', 'DESC');
$this->db->limit(1);
$query = $this->db->get('sites');

return   $query->result_array();
}




Theme © iAndrew 2016 - Forum software by © MyBB