joebudden Posted April 22, 2007 Share Posted April 22, 2007 hi guys i have this php script and want to run it 7 times to create 7 random teams.. anyone know how i wud achieve this thanx // random team generation // // generate first random team // random team id variable $teamidRAND = rand(1,1000000); // update tournament table SQL $tournamentSQL = "INSERT INTO tournament(tournamentid,teamid) VALUES ($gameid,$teamidRAND)"; mysql_query($tournamentSQL) or die (mysql_error()); // update draw table SQL $drawSQL = "INSERT INTO $draw(teamid,tournamentid) values ($teamidRAND,$gameid)"; mysql_query($drawSQL) or die (mysql_error()); // SQL to select 5 random player Ids $playerSQL = "SELECT playerId FROM $temp ORDER BY RAND() LIMIT 5"; $playerRES = mysql_query($playerSQL) or die(mysql_error()); //check if there is data to be added if (mysql_num_rows($playerRES) == 5) { while($row = mysql_fetch_assoc($playerRES)) { // random playsfor variables $fitnessRAND = rand(6,10); $trainingRAND = rand(1,5); $tacticsRAND = rand(1,5); $teamtalkRAND = rand(1,5); $moraleRAND = rand(6,10); $mediaRAND = rand(1,5); // random player Id $randomPlayer = $row["playerId"]; // update playsfor table with players SQL $playsforSQL = "INSERT INTO playsfor ( playerId, teamid, playerCaptain, playerStarting, playerFitness, playerTraining, playerTactics, playerTeamTalk, playerMorale, playerMedia ) VALUES ( $randomPlayer, $teamidRAND, 0, 0, $fitnessRAND, $trainingRAND, $tacticsRAND, $teamtalkRAND, $moraleRAND, $mediaRAND )"; mysql_query($playsforSQL) or die (mysql_error()); // delete from temp table SQL $delPlayerSQL = "DELETE FROM $temp WHERE playerId=$randomPlayer"; mysql_query($delPlayerSQL) or die (mysql_error()); } // end of while to get players // SQL to select random captain $captainSQL = "SELECT playerId FROM $temp ORDER BY RAND() LIMIT 1"; $captainRES = mysql_query($captainSQL) or die(mysql_error()); while($row = mysql_fetch_assoc($captainRES)) { $randomCaptain = $row["playerId"]; // update playsfor table with captain SQL $playsforSQL = "INSERT INTO playsfor ( playerId, teamid, playerCaptain, playerFreeKicks, playerPenalties, playerStarting, playerFitness, playerTraining, playerTactics, playerTeamTalk, playerMorale, playerMedia ) VALUES ( $randomCaptain, $teamidRAND, 1, 1, 1, 0, $fitnessRAND, $trainingRAND, $tacticsRAND, $teamtalkRAND, $moraleRAND, $mediaRAND )"; mysql_query($playsforSQL) or die (mysql_error()); // delete from temp table SQL $delCaptainSQL = "DELETE FROM $temp WHERE playerId=$randomCaptain"; mysql_query($delCaptainSQL) or die (mysql_error()); } // end of while to get captain // get a players name from that team $getPlayerSQL = "SELECT playerSurname FROM player,playsfor WHERE player.playerId = playsfor.playerId AND teamid=$teamidRAND AND playerCaptain='1' ORDER BY RAND() LIMIT 1"; $getPlayerRES = mysql_query($getPlayerSQL); $getPlayerARR = mysql_fetch_assoc($getPlayerRES); $getPlayerSTR = $getPlayerARR["playerSurname"]; $letter = $getPlayerSTR{0}; $getNameSQL = "SELECT randomName FROM randomNames WHERE randomName LIKE '$letter%' ORDER BY RAND() LIMIT 1 "; $getNameRES = mysql_query($getNameSQL); $getNameARR = mysql_fetch_assoc($getNameRES); $getNameSTR = $getNameARR["randomName"]; if(mysql_num_rows($getNameRES) != 0) { // concatenate random team name $randTeamName = $getPlayerSTR."\'s ".$getNameSTR; } else { // get an alternative team name $alternativeSQL = "SELECT randomName FROM randomNames ORDER BY RAND() LIMIT 1"; $alternativeRES = mysql_query($alternativeSQL); $alternativeARR = mysql_fetch_assoc($alternativeRES); $alternativeSTR = $alternativeARR["randomName"]; // concatenate random team name $randTeamName = $getPlayerSTR."\'s ".$alternativeSTR; } // random team variables $teamTrainingRAND = rand(1,5); $teamTacticsRAND = rand(1,5); $teamTeamTalkRAND = rand(1,5); $teamMediaRAND = rand(1,5); // get a manager $managerSQL = "SELECT managerName FROM manager ORDER BY RAND() LIMIT 1"; $managerRES = mysql_query($managerSQL); $managerARR = mysql_fetch_assoc($managerRES); $managerName = $managerARR["managerName"]; // get an assistant manager $assManSQL = "SELECT managerName FROM manager WHERE managerName NOT IN ('$managerName') ORDER BY RAND() LIMIT 1"; $assManRES = mysql_query($assManSQL); $assManARR = mysql_fetch_assoc($assManRES); $assManName = $assManARR["managerName"]; // update team table SQL $teamSQL = "INSERT INTO team (teamid,teamname,manager,assManager,training,tactics,teamtalk,media) VALUES ( $teamidRAND, \"$randTeamName\", \"$managerName\", \"$assManName\", $teamTrainingRAND, $teamTacticsRAND, $teamTeamTalkRAND, $teamMediaRAND )"; mysql_query($teamSQL) or die (mysql_error()); // end of team generation Quote Link to comment https://forums.phpfreaks.com/topic/48196-quick-question/ Share on other sites More sharing options...
Daniel0 Posted April 22, 2007 Share Posted April 22, 2007 for($i=0; $i<7; $i++=) { // do stuff } will do something seven times. Quote Link to comment https://forums.phpfreaks.com/topic/48196-quick-question/#findComment-235603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.