Guest 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 Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/ 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 Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513664 Share on other sites More sharing options...
Guest Posted April 10, 2008 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>"; } } Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513667 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. Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513674 Share on other sites More sharing options...
Guest Posted April 10, 2008 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? Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513685 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, Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513773 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 Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513800 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 Link to comment https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.