在这个锦标赛赛程算法上苦苦挣扎。
代码运行良好,但我需要帮助插入数据到mysql我似乎不能访问的$varables。
任何调整由一个php专业人士非常感谢…
$teamnames = "Arsenal|Tottenham|Leeds|Man United|Liverpool";
# XXX check for int
print show_fixtures(isset($_GET['teams']) ? nums(intval($_GET['teams'])) : explode("|", trim($teamnames)));
function nums($n) {
$ns = array();
for ($i = 1; $i <= $n; $i++) {
$ns[] = $i;
}
return $ns;
}
function show_fixtures($names) {
$teams = sizeof($names);
print "<p>Fixtures for $teams teams.</p>";
// 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);
}
}
// 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 "<hr><p>Round " . ($i + 1) . "</p>\n";
foreach ($rounds[$i] as $r) {
print $r . "<br />";
}
print "<br />";
}
print "<hr>Second half is mirror of first half";
$round_counter = sizeof($rounds) + 1;
for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
print "<hr><p>Round " . $round_counter . "</p>\n";
$round_counter += 1;
foreach ($rounds[$i] as $r) {
print flip($r) . "<br />";
}
print "<br />";
}
print "<br />";
if ($ghost) {
print "Matches against team " . $teams . " are byes.";
}
}
function flip($match) {
$components = split(' v ', $match);
return "$components[1]" . " v " . "$components[0]";
}
function team_name($num, $names) {
$i = $num - 1;
if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
return trim($names[$i]);
} else {
return "BYE";
}
}发布于 2012-01-30 09:13:02
我不能完全确定您所挂起的是什么(您确实应该在您的问题中更具体,正如FAQ所指定的),但我怀疑这是一个范围问题。
在函数中设置变量时,只能在该函数中访问该变量。例如:
function do_something() {
$a = 'something!';
}
do_something();
echo $a;这应该会导致PHP通知您PHP不知道它试图echo的作用域中的$a是什么。现在,如果我修改这个脚本...
$a = '';
function do_something() {
global $a; // Lets PHP know we want to use $a from the global scope
$a = 'something!';
}
do_something();
echo $a;这将起作用并输出"something!",因为$a是在函数外部的作用域中“定义”的。
您可以在PHP文档中阅读有关变量范围的更多信息:http://php.net/manual/en/language.variables.scope.php
现在,您需要注意的另一件事是输出用户数据。在您的脚本中,直接从$_GET获取数据并将其打印到页面。为什么这很糟糕?有人可以将一些漂亮的JavaScript注入你的页面(或任何他们想要的东西),窃取用户的会话。在需要输出变量的任何时候,都应该使用htmlspecialchars()。即使这只是一个团队名称,你也永远不知道什么时候某个团队会在其中添加一个;、<、>或其他保留字符。
最后,我强烈建议不要将逻辑与计算混为一谈。让你的程序弄清楚所有的事情,然后循环你的输出数据。您应该能够将这类问题的全部数据保存在一个很好的关联数组中,或者保存在您想出的某个巧妙的对象中。
https://stackoverflow.com/questions/9058284
复制相似问题