Databaseudtræk
Hey alle.Jeg har fundet et stykke kode som gør præcis det jeg skal bruge, men da jeg ikke er super stiv i PHP, tænkte jeg om der er nogen der var friske på en opgave:
Jeg har en tabel kaldet teams. team_id og team_nick indgår. Koden i sig selv tager antal teams, blander et kampprogram så alle spiller mod alle. Det jeg vil have en af jer til, er at ændre i koden, sådan at den vil trække holdende ud fra min database, og generere et kampprogram udfra disse data.
Nogen friske? :)
y^
---------------------------------------
<?php
mysql_connect ("192.168.1.105","root","bussemand");
mysql_select_db ("league");
$query = mysql_query("SELECT * FROM teams WHERE team_division = 1");
while ($data = mysql_fetch_array($query)){
};
function main() {
// Find out how many teams we want fixtures for.
$teams = 6;
// 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] = ($home + 1) . " v " . ($away + 1);
}
}
// Interleave so that home and away games are fairly evenly dispersed.
$interleaved = array();
for ($i = 0; $i < $totalRounds; $i++) {
$interleaved[$i] = array();
}
$evn = 0;
$odd = ($teams / 2);
for ($i = 0; $i < sizeof($rounds); $i++) {
if ($i % 2 == 0) {
$interleaved[$i] = $rounds[$evn++];
} else {
$interleaved[$i] = $rounds[$odd++];
}
}
$rounds = $interleaved;
// Last team can't be away for every game so flip them
// to home on odd rounds.
for ($round = 0; $round < sizeof($rounds); $round++) {
if ($round % 2 == 1) {
$rounds[$round][0] = flip($rounds[$round][0]);
}
}
// Display the fixtures
for ($i = 0; $i < sizeof($rounds); $i++) {
print "<p>Round " . ($i + 1) . "</p>\n";
foreach ($rounds[$i] as $r) {
print $r . "<br />";
}
print "<br />";
}
print "<br />";
if ($ghost) {
print "Kampe imod " . $teams . " er nulkampe.";
}
print ("<p>Spejlbillede for anden kamp mod samme hold.</p>");
}
function flip($match) {
$components = split(' v ', $match);
return $components[1] . " v " . $components[0];
}
main();
?>