MrOnline Posted January 14, 2003 Share Posted January 14, 2003 I am trying to grab some fields out of my database and shuffle them to play eachother. include ("db.php"); $read_teams="SELECT teamname FROM ptg_teams"; $read_teams_result=mysql_query($read_teams); $a = mysql_num_rows(mysql_query("SELECT * FROM ptg_teams")); $x = 0; while($x < $a) { $data=mysql_fetch_array($read_teams_result); $rand_keys = array_rand ($data, 2); $randteams[$x]=$data[$rand_keys[0]]."n"; $x++; $data=mysql_fetch_array($read_teams_result); $randteams[$x]=$data[$rand_keys[1]]."n"; $x++; } $i=0; while($i < $a) { $rand_keys = array_rand ($randteams, $a); print $randteams[$rand_keys[$i]]; $i++; } while() { $rand_keys++ echo "<br>"; } This is what I have but it sorts em in pairs wrong. It just double lists the names, such as: Team1 Team1 Team 2 Team 2 when I need it to mix em up so team1 and team2 match up together. Link to comment https://forums.phpfreaks.com/topic/45-randomizing-a-mysql-query/ Share on other sites More sharing options...
lemke411 Posted October 16, 2007 Share Posted October 16, 2007 Try using this: ORDER BY RAND() Link to comment https://forums.phpfreaks.com/topic/45-randomizing-a-mysql-query/#findComment-370898 Share on other sites More sharing options...
fenway Posted October 17, 2007 Share Posted October 17, 2007 You can just shuffle a list of UIDs in php and store the result back. Link to comment https://forums.phpfreaks.com/topic/45-randomizing-a-mysql-query/#findComment-371531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.