Page 1 of 1
shuffle not shuffling...
Posted: Wed Jul 05, 2006 11:47 am
by mjmacarty
...coder does not know how to make it.
Code: Select all
$result = mysql_query($query, $conn) or die(mysql_error());
for ( $i=0; $i<3; $i++){
$row = mysql_fetch_array($result);
shuffle(array_keys($row));
This aways returns the same three products. Obviously I am missing the finer points of the old shuffle function.
Posted: Wed Jul 05, 2006 11:54 am
by feyd
shuffle() manipulates the array given to it. Since you are not assigning the results of array_keys() to a variable, therefore the array is never actually changed.
Posted: Wed Jul 05, 2006 12:37 pm
by mjmacarty
You mean like this?
Code: Select all
$result = mysql_query($query, $conn) or die(mysql_error());
for ( $i=0; $i<3; $i++){
$row = mysql_fetch_array($result);
$do_the_hustle = array_keys($row);
shuffle($do_the_hustle);
Is that different from the original code?
Posted: Wed Jul 05, 2006 12:40 pm
by feyd
$do_the_hustle will be the shuffled array, so yes.
Posted: Wed Jul 05, 2006 1:21 pm
by mjmacarty
OK. I think I see what's happening. I get random key values from the array. What I want is random "rows" from the array, as in random results from a query.
Posted: Wed Jul 05, 2006 1:27 pm
by John Cartwright
you could get random results using the query instead of having php randomize your results..
ie..
Posted: Wed Jul 05, 2006 1:39 pm
by mjmacarty
This seems easier to me (although I wouldn't mind knowing how to do this with php):
Code: Select all
$query = "SELECT * FROM books ORDER BY rand() LIMIT 3;";
$result = mysql_query($query, $conn) or die(mysql_error());
for ( $i=0; $i<3; $i++){
$row = mysql_fetch_array($result);
This is the effect I was looking for. Thanks for helping me sort through this.
Posted: Wed Jul 05, 2006 1:40 pm
by mjmacarty
Jcart wrote:you could get random results using the query instead of having php randomize your results..
ie..
Heh, thanks. I thought of it in the shower!