Welcome Guest, Not a member yet? Register   Sign In
is there any super easy cool trick to get the next id in codeigniter :))
#9

[eluser]David Johansson[/eluser]
[quote author="mimran" date="1248559053"]Hi,

Not an elegant way however keeping the logic very simple, basically what you wanted.

increment the id the way you wanted (i.e. id+1 but just run a check against id's to see if it exists)

that will do the trick.

regards[/quote]

That's a terrible idea! what if it's the last client?
I would suggest something similar to what's already suggested:

[quote author="NateL" date="1248501168"]Since you have the ID in the URI, you will have to run this very simple query:

SELECT * FROM `clients` WHERE `clientid` > *segment 3* LIMIT 1;

That query will grab the next highest ID, even if you are missing ID's between. The LIMIT 1 is very important in this.[/quote]

But you really need to use an ORDER BY statement!

In active record it would be:
Code:
$query = $this->db->select('id')->order_by('id', 'asc')->get_where('clients', array('id >' => $id), 1, 0);
if($query->num_rows == 1)
{
    $next_id = $query->row()->id;
}
else
{
    $next_id = FALSE;
}


Messages In This Thread
is there any super easy cool trick to get the next id in codeigniter :)) - by El Forum - 07-26-2009, 01:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB