Jump to content


Photo

Randomizing a MYSQL query


  • Please log in to reply
2 replies to this topic

#1 MrOnline

MrOnline
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 January 2003 - 08:03 PM

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.

#2 lemke411

lemke411
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 16 October 2007 - 06:02 PM

Try using this:
ORDER BY RAND()
Joseph Lemke
Web Programmer / Developer

Glacial Wood

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 October 2007 - 02:08 PM

You can just shuffle a list of UIDs in php and store the result back.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users