Question

I need help with my football manager tournament. I am trying to insert all teams that are registered in the game into a table called "timetable" randomly. The database will have the columns id, team1, team2, date, time. I know how to do this manually but it will take to much time if it's like 50 teams and to figure out wish team they haven't challenge. And how to make that all teams play one match everyday.

<?php
mysql_query("INSERT INTO timetable (id, team1, team2, date, time) VALUES ('','Blue','Red','2011-02-06','18.00')")
?>

I know my English is not the best but I hope you get it. Thanks any way,

Was it helpful?

Solution

As far as I have understood your question, you want every team to play against every other team? This is simple. Just loop over the teams.

<?php
$teams = array('Red Team', 'Blue Team', 'Yellow Team', 'Green Team');
foreach($teams as $team_one) {
  foreach($teams as $team_two) {
    mysql_query("INSERT INTO timetable (team1, team2) VALUES ('$team_one', '$team_two')");
  }
}
?>

You might also want to write a match date calculation algorithm.

If your $teams array is user supplied you probably want to escape your inputs with mysql_real_escape_string() for security reasons.

mysql_query("INSERT INTO timetable (team1, team2) VALUES ('".mysql_real_escape_string($team_one)."', '".mysql_real_escape_string($team_two)."')");

For details about security, please read the PHP manual about mysql_real_escape_string() and/or google about SQL injection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top