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

[eluser]artlover[/eluser]
Hi friends,

is there any super easy cool trick to get the next id in codeigniter :-)

for example; my page is .. .... /work/client/38

38 is id and i want to use a Next Client link. how can it be done? id+1 is not healthy, id can be so different...

Thanks!!!
#2

[eluser]jedd[/eluser]
How is 39 not healthy?

39 is the obvious candidate for the next item after item #38.

If you have a different algorithm, perhaps you should share it - and then someone can probably quite easily come up with the PHP for same.
#3

[eluser]artlover[/eluser]
it is actually about mysql. for example you have records with id ..., 37, 38, 39, 40, 41, ... and when you delete the 38 id record, you have new ids are like ..., 37, 39, 40, 41, ... so if you try to make Next Project with (id+1) from 37, you just can not get anything.

I thought maybe there is a function or something at codeigniter that gives real next id at db.
#4

[eluser]jedd[/eluser]
Ahh, gotcha. Nah, you'll have to write your own model method for that particular table - using ORDER, LIMIT and OFFSET. It's not terribly tricky, though no idea how you'd do it with AR. Have a play with the raw SQL and post back if you get stuck.
#5

[eluser]John_Betong[/eluser]
[quote author="artlover" date="1248498414"]Hi friends,

is there any super easy cool trick to get the next id in codeigniter :-)

for example; my page is .. .... /work/client/38

38 is id and i want to use a Next Client link. how can it be done? id+1 is not healthy, id can be so different...

Thanks!!![/quote]
 
 
I would be tempted to set a $_SESSION['last_id'] variable then have a controller/next function which sets your SQL statement.
 
No doubt there are numerous other ways but this will get the job done and you can move on to your next problem Smile
 
 
 
#6

[eluser]xzela[/eluser]
you could probably write your own next() function to get all of the next ids

Code:
$this->load->model('client_model')
$data['client_ids'] = $this->client_model->getClientIds();


then, location the current id and walk up/down the array tree based on where you're at.


it could work.
#7

[eluser]NateL[/eluser]
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.
#8

[eluser]mimran[/eluser]
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
#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;
}
#10

[eluser]mimran[/eluser]
Really depends on how you coded.
Count your clients first ............. make sure you only increment upto the highest number......
And it was only suggested as the guy wanted to do it this way ........... and well it is easily doable.............




Theme © iAndrew 2016 - Forum software by © MyBB