2012-01-30 96 views
0

用這個錦標賽夾具算法掙扎。Fixtures PHP算法

的代碼工作完美,但我需要幫助插入數據到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"; 
    } 
} 
+1

給定的代碼究竟有什麼問題?什麼不行? – Borealid 2012-01-30 01:08:52

+0

它在哪裏爆炸? – jbnunn 2012-01-30 01:09:20

+0

代碼的作品我只需要幫助插入 – Webby 2012-01-30 01:14:56

回答

2

我不完全確定你掛了什麼(你的問題應該更具體一些,如FAQ所述),但我懷疑這是一個範圍問題。

當您在函數中設置變量時,該變量只能在該函數內訪問。例如:

function do_something() { 
    $a = 'something!'; 
} 
do_something(); 
echo $a; 

這將導致PHP通知告訴你,PHP不知道什麼$a是在範圍內,它正試圖echo。現在,如果我修改這個劇本......

$a = ''; 
function do_something() { 
    global $a; // Lets PHP know we want to use $a from the global scope 
    $a = 'something!'; 
} 
do_something(); 
echo $a; 

這將工作和輸出「的東西!」,因爲$a正在對管理的功能之外的「定義」。

你可以閱讀更多關於PHP文件中的變量範圍:http://php.net/manual/en/language.variables.scope.php

現在,你需要注意別的東西輸出的用戶數據。在腳本中,您直接從$_GET獲取數據並將其輸出到頁面。爲什麼這不好?有人可能會在您的頁面(或他們想要的任何內容)中注入一些漂亮的JavaScript,並竊取用戶的會話。無論何時您需要輸出變量,您都應該使用htmlspecialchars()。即使它只是一個球隊的名字,你永遠不會知道什麼時候一些球隊將會在其中保留一個;<>或其他保留字符。

最後,我強烈建議不要將您的邏輯與您的計算混合。讓你的程序把所有東西都弄清楚,然後循環輸出數據。你應該能夠將這類問題的整個數據保存在一個很好的關聯數組中,或者你想出的一些狡猾的對象。

+1

PHP不是JavaScript。你的第二個例子不起作用。您必須使用'global $ a;'使其成爲全局/覆蓋全局$ a。 – Saxoier 2012-01-30 01:28:44

+0

@Saxoier,好,趕上,謝謝。 – Brad 2012-01-30 01:36:18

+0

感謝您的更新似乎是一個全球性問題..我以前沒有遇到過這個......非常感謝! – Webby 2012-01-30 01:48:31