![]() |
Random Query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Random Query (/showthread.php?tid=25396) |
Random Query - El Forum - 12-10-2009 [eluser]Philo01[/eluser] Hi there! I'm trying to pull random data from a table, and found the following "solution": http://www.derekallard.com/blog/post/ordering-database-results-by-random-in-codeigniter/ Code: $this->db->select('name'); Now when I apply this method on my application, I get the following error: Quote:A PHP Error was encountered My model returns the array to my controller: Code: $this->db->where('activated', '1'); Next, my Controller sends the array to a view: Code: foreach($ads as $a){ When I use this code without the shuffle part it works perfect, only it doesn't select randomly. When I use the shuffle solution, I get that error. Anyone who can help me out ![]() Kind regards, Philo Random Query - El Forum - 12-10-2009 [eluser]CtheB[/eluser] Sorry i don't use active record in codeigniter so i can't help you. It is much easier to do it like this: $query = $this->db->query("SELECT * FROM orders where activated = 1 AND expired = 0 ORDER BY rand() "); return $query->result_array(); Random Query - El Forum - 12-10-2009 [eluser]Philo01[/eluser] [quote author="CtheB" date="1260490524"]Sorry i don't use active record in codeigniter so i can't help you. It is much easier to do it like this: $query = $this->db->query("SELECT * FROM orders where activated = 1 AND expired = 0 ORDER BY rand() "); return $query->result_array();[/quote] Thanks mate! ![]() Random Query - El Forum - 12-10-2009 [eluser]jedd[/eluser] I believe (but have not tested or confirmed) that it is preferable, for performance reasons, to query the database on the number of rows in the table, use a local PHP random function against that figure, and then extract that specific record from the database. Random Query - El Forum - 08-27-2012 [eluser]WhitecastlePT[/eluser] Hello, here's an example with active records: Works like a charm ![]() Code: function getAllFotos() |