Welcome Guest, Not a member yet? Register   Sign In
shuffle data retrieved from database
#1

[eluser]chefnelone[/eluser]
Hello,

I'd like to shuffle the elements of $data array which is retrieved data from the user table. I tried with shuffle but cant get it working.

Is there a way to do this?

Code:
$data = $this->db->get('alojamientos')->result();        
return shuffle($data);

Thanks
#2

[eluser]danmontgomery[/eluser]
Code:
$data = $this->db->order_by('random')->get('alojamientos')->result();
#3

[eluser]chefnelone[/eluser]
[quote author="noctrum" date="1287614265"]
Code:
$data = $this->db->order_by('random')->get('alojamientos')->result();
[/quote]

I tried but I've got the error message:

ErrorException [ Fatal Error ]: Error Number: 1054 / Unknown column 'random' in 'order clause' / SELECT * FROM (`alojamientos`) ORDER BY `random` LIMIT 5
#4

[eluser]danmontgomery[/eluser]
sorry,

Code:
$this->db->order_by('[some field]', 'random')->get('alojamientos')->result();

random needs to be the 2nd parameter, not the first... You just need to specify any field.
#5

[eluser]CroNiX[/eluser]
Heres a good article explaining how order by rand can eat memory with large tables and a great workaround (order by rand creates a temp table of all results first...which might not be good)
http://www.paperplanes.de/2008/4/24/mysql_nonos_order_by_rand.html




Theme © iAndrew 2016 - Forum software by © MyBB