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

[eluser]garymardell[/eluser]
Davids way is the better option, and you do not even need to count the number of clients really as you can just check the number of rows. If no rows are returned your at the end.
#12

[eluser]NateL[/eluser]
[quote author="David Johansson" date="1248611542"]
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;
}
[/quote]

Thanks for cleaning it up and putting it into CI Active Record for me Smile

Now, I'm not doubting you - just curious why you need to use ORDER BY ?

When you say "select by id greater than 5 limit 1" - it's pretty obvious to me it's going to look for the next highest id above 5, so why sort one result?
#13

[eluser]artlover[/eluser]
hey, thanks for great posts Smile

David's way is working brilliant! Smile

I also added a code like;
Code:
<?php if ($next_project) { ?>
      //next project link

so next project link is not displaying at last project page.


ps, I love codeigniter, it is fun to develop project with ci, also I love this forum and you helpful guys : ) thanks again...
#14

[eluser]David Johansson[/eluser]
[quote author="NateL" date="1248643086"][quote author="David Johansson" date="1248611542"]
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;
}
[/quote]

Thanks for cleaning it up and putting it into CI Active Record for me Smile

Now, I'm not doubting you - just curious why you need to use ORDER BY ?

When you say "select by id greater than 5 limit 1" - it's pretty obvious to me it's going to look for the next highest id above 5, so why sort one result?[/quote]

Well by SQL standars the rows don't come in a particular order (it's defined by "set theory"), they can for what you know come like (12, 5, 37, 2, 16...). In actuality MySQL returns the rows in the order they where entered unless you have done any reordering with "ALTER TABLE... ORDER BY...". To sum up: to be 100% sure you must use an ORDER BY statement though it will with 99% probability work anyway. You should really always use order by whenever the order makes any difference.




Theme © iAndrew 2016 - Forum software by © MyBB