Randomize/Group/Paste

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
NicEJoB
Forum Newbie
Posts: 14
Joined: Tue Jul 15, 2003 8:31 pm

Randomize/Group/Paste

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post 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.
Post Reply