Jump to content

Randomizing a MYSQL query


MrOnline

Recommended Posts

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
Share on other sites

  • 4 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.