Hi all,
I am attempting to sort out this code, so far I can build the fixtures list and insert them to my database. However I need to add a date to the games to be played, can anyone help me with this issue?
<?php
// CONNECTING TO DATABASE
$dbhost = 'localhost';
$dbuser = '***';
$dbpass = '***';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '***';
mysql_select_db($dbname);
$table = "eden_prem";
$sql = "SELECT * FROM $table";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$names[] = $row['team_name'];
$ids[] = $row['team_id'];
}
mysql_free_result($result);
echo show_fixtures($names,$ids);
function nums($n) {
$ns = array();
for ($i = 1; $i <= $n; $i++) {
$ns[] = $i;
}
return $ns;
}
function show_fixtures($names,$ids) {
$teams = sizeof($names);
// If odd number of teams add a "ghost".
$ghost = false;
if ($teams % 2 == 1) {
$teams++;
$ghost = true;
}
// Generate the fixtures using the cyclic algorithm.
$totalRounds = $teams - 1;
$matchesPerRound = $teams / 2;
$rounds = array();
for ($i = 0; $i < $totalRounds; $i++) {
$rounds[$i] = array();
}
for ($round = 0; $round < $totalRounds; $round++) {
for ($match = 0; $match < $matchesPerRound; $match++) {
$home = ($round + $match) % ($teams - 1);
$away = ($teams - 1 - $match + $round) % ($teams - 1);
// Last team stays in the same place while the others
// rotate around it.
if ($match == 0) {
$away = $teams - 1;
}
$rounds[$round][$match] = team_name($home + 1, $names)
. " v " . team_name($away + 1, $names);
mysql_query ("INSERT INTO se_team_fixtures (home_team_id, home_team_name, away_team_id, away_team_name, league) VALUES ('$ids[$home]', '$names[$home]', '$ids[$away]', '$names[$away]', 'eden_prem')") or die(mysql_error());
mysql_query ("INSERT INTO se_team_fixtures (home_team_id, home_team_name, away_team_id, away_team_name, league) VALUES ('$ids[$away]', '$names[$away]', '$ids[$home]', '$names[$home]', 'eden_prem')") or die(mysql_error());
}
}
}
function team_name($num, $names) {
$i = $num - 1;
if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
return trim($names[$i]);
} else {
return $num;
}
}
print "<p>Fixtures for $table have been created.</p>";
// CLOSE CONNECTION TO DATABASE
mysql_close($conn);
?>