CodeIgniter Forums
Get last item in list - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Get last item in list (/showthread.php?tid=68272)



Get last item in list - wolfgang1983 - 06-18-2017

Hi, In my database table I would like to be able to get the last row that was created.

Have I got this model function correct. Just unsure of my self not sure if there is another easier way?

PHP Code:
public function getpostinfofordeleteupdate()
{
 
   $query $this->db->order_by('datecreated''desc')
         
     ->limit(1)
         
     ->get($this->db->dbprefix 'posts');

 
   return $query->row_array();

Thanks for your time.


RE: Get last item in list - Martin7483 - 06-18-2017

Sorting as you are doing here is the only way I can think of todo that.


RE: Get last item in list - wolfgang1983 - 06-18-2017

(06-18-2017, 03:27 AM)Martin7483 Wrote: Sorting as you are doing here is the only way I can think of todo that.

Thank you. It works fine just was unsure if any other way using DB stuff in model.


RE: Get last item in list - Martin7483 - 06-18-2017

If you only want the ID of the record you could use a select max to get it