I researched over the internet, but could not find anything...
I have a mysql db, and records at a table, and I need to get random record from this table at every page load. how can I do that? Is there any func for that?
Appreciate! thanks
SORTED: link: http://www.derekallard.com/blog/post/ordering-database-results-by-random-in-codeigniter/
$this->db->select('name');
$query = $this->db->get('table');
$shuffled_query = $query->result_array();
shuffle ($shuffled_query);
foreach ($shuffled_query as $row) {
echo $row['name'] . '<br />';
}
The ORDER BY RAND() clause returns random records! You can limit records also using LIMIT.
I don't know about codeigniter, but getting a random dataset is
The relevant part is "
ORDER BY RAND()
", obviously.Codeigniter provides the ability to order your results by 'RANDOM' when you run a query. For instance
I've used this before and found it to work fine. Hope that helps
I use codeigniter with datamapper. This is the code which I use to get a record randomly from table
Advertiser
:Getting random record from large table is very expensive. But bellow this code is very effective ..
This will be helpful ...