gdfhghjdfghgfhf Posted April 10, 2008 Share Posted April 10, 2008 is it possible to randomize the order of a query? i want to use this to display a random entry of a table Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 10, 2008 Share Posted April 10, 2008 not for for a direct mysql query, but you can try php's shuffle() function. http://uk.php.net/manual/en/function.shuffle.php Quote Link to comment Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 10, 2008 Author Share Posted April 10, 2008 hmmm that site you gave me won't work can you give me an example how i could randomize this query? It's currently ordered by ids $f5 = "$bbuserinfo[field5]"; $query = "SELECT * FROM quebec_userfield WHERE field5='$f5' ORDER BY userid DESC"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $id = $row["userid"]; $query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $nom = $row["username"]; echo "<a href=\"member.php?username=$nom\">"; echo $nom; echo "</a><br>"; } } Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 10, 2008 Share Posted April 10, 2008 strange, try www.php.net/shuffle also - try this: while($row = mysql_fetch_array($res)) { $res[] = $row; } shuffle($res); foreach($res As $row){ $id = $row["userid"]; $query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $nom = $row["username"]; echo "<a href=\"member.php?username=$nom\">"; echo $nom; echo "</a><br>"; } may be long way around but should work. Quote Link to comment Share on other sites More sharing options...
gdfhghjdfghgfhf Posted April 10, 2008 Author Share Posted April 10, 2008 hmmm i'm getting an "unexpected end" error what part of the script am i supposed to replace with your code, just to be sure? Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 10, 2008 Share Posted April 10, 2008 Opps i missed an ending parenthesis, try this: $f5 = "$bbuserinfo[field5]"; $query = "SELECT * FROM quebec_userfield WHERE field5='$f5' ORDER BY userid DESC"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $res[] = $row; } shuffle($res); foreach($res As $row){ $id = $row["userid"]; $query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $nom = $row["username"]; echo "<a href=\"member.php?username=$nom\">"; echo $nom; echo "</a><br>"; } } replace the last code you gave me with this. hope this helps, Quote Link to comment Share on other sites More sharing options...
sasa Posted April 10, 2008 Share Posted April 10, 2008 is it possible to randomize the order of a query? i want to use this to display a random entry of a table add 'ORDER BY RAND()' in your query Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 10, 2008 Share Posted April 10, 2008 Also, check this out: http://www.phpfreaks.com/forums/index.php/topic,125759.0.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.