Page 1 of 1

Randomize/Group/Paste

Posted: Tue Jul 29, 2003 11:47 pm
by NicEJoB
I currently have a PHP script that takes a name that was typed into a textbox and adds it to a MySQL server. Its setup to only accept 24 names for my purpose. When the names are in the server, id like it to randomize the names and place them into 8 groups of 3, adding those groups to a webpage, i have tried something like this, but failed w/o luck

Code: Select all

<?php 
 
mysql_connect("dbhost", "dbusername", "dbpassword"); 
$db = mysql_select_db("dbname"); 
$result = mysql_query("SELECT name FROM ruc_players"); 
$arr = mysql_fetch_array($result, MYSQL_NUM);
srand((float)microtime()*1000000); 
shuffle($arr); 
$team = 1; 
while (count($arr) > 0) 
&#123; 
$name = array_pop($arr);
     mysql_query("UPDATE ruc_players SET team='$team' WHERE name='$name'"); 
$team = (($team == 7) ? 1 : $team + 1);
&#125; 
 
?>
ruc_players being the sub db of the db. ideas or help greatly appreciated

...

Posted: Wed Jul 30, 2003 12:45 am
by kettle_drum
just take all the names out of the database and place them in a an array, then use shuffle() to make them random. Once you have done this, either split the array into smaller arrays, or print them to screen in groups of 3, or do whatever else you need to do.