Welcome Guest, Not a member yet? Register   Sign In
Randomize Active Record Results
#6

[eluser]Aken[/eluser]
The DB methods that return more than one result will ALWAYS return an array. $query->result() will return an array of objects. $query->result_array() will return an array of arrays. If you're looking to keep each row as an object, all you have to do is randomize the $query->result() array, which is as simple as:

Code:
$query = $this->db->get($table);
$rows = $query->result();

// Pull one random item.
$random = $rows[array_rand($rows)];

// Randomize the entire result array.
shuffle($rows);

If you want to do that in a DB method like your example, then search around for extending the database and add it yourself! Smile


Messages In This Thread
Randomize Active Record Results - by El Forum - 05-17-2012, 09:09 PM
Randomize Active Record Results - by El Forum - 05-17-2012, 09:22 PM
Randomize Active Record Results - by El Forum - 05-18-2012, 12:50 PM
Randomize Active Record Results - by El Forum - 05-18-2012, 01:03 PM
Randomize Active Record Results - by El Forum - 05-22-2012, 12:55 PM
Randomize Active Record Results - by El Forum - 05-22-2012, 02:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB