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

[eluser]PhilTem[/eluser]
I'm not sure if I got your question, right: Do you want to randomize the rows you retrieve from the database, or do you want to first get a set of data from the DB and then randomize this?

For the first case, you can of course use MySQL's RAND() but you may also create a set of 10 random integers serving as primary keys and only grab these. Getting that random primary keys array is rather simple with PHP Wink
Of course you need to ensure, that the randomly generated keys are valid keys and that you don't have any keys twice or three times, but as just mentioned, it's rather simple.

The second one might be similar to the first one, i.e. you have your query result handle $query and then just use a random number n (or 10) to retrieve the n-th row of your results like
Code:
$row = $query->row($random_number); // for object
$row = $query->row_array($random_number); // for array

Maybe I'm not far off what you were aiming to get and hopefully it will lead you into the right direction.

PS: None of the ways mentioned above changes the type of data you get from the DB, i.e. it's still an array or an object, depending on how you want it.


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