2017-04-23 25 views
-1

生成緩存靜態頁面時,如何基於複雜SQL查詢的內容動態重新排列六個PHP代碼塊?動態地重新排列SQL查詢結果上PHP代碼塊的顯示順序?

我有六個功能完全相同的PHP代碼塊(下面包含一個例子),每個代碼塊都顯示六場比賽中的前五名錶現。

我將PHP代碼包裝到function中,但遇到SQL查詢失敗的問題。這是通過發送數據庫連接器變量作爲函數參數來解決的,而且最初的監督對於我來說是愚蠢的,因爲它是愚蠢的。

這裏是基本的SQL查詢,我設計用來爲六個遊戲中的一個遊戲產生一個平均分數,其他五個在功能上是相同的。

$my_var1 = mysqli_query($connection, "SELECT AVG(score) FROM (SELECT g1_score AS score FROM game_1) TMP"); 

這裏是代碼,自定義函數用於顯示六個前5分塊之一:

function t5_game_1($connection) { 
print "<p>Game 1</p>\n"; 
print "<table style='margin: 0 auto;'>\n"; 
$count = 0; 
$A = mysqli_query($connection, " 
SELECT players.Pname 
    , game_1.g1_score 
    , game_1.DVN 
    FROM players 
    LEFT 
    JOIN game_1 
    ON players.PID = game_1.PID 
WHERE game_1.g1_seen = 'Y' 
ORDER 
    BY game_1.g1_score DESC 
LIMIT 5 
    "); 
IF ($AA = mysqli_fetch_array($A)) { 
    do { 
     print "<tr><td class='w3-left-align'>{$AA[0]}</td><td class='w3-right-align'>{$AA[1]}</td>"; 
     SWITCH ($AA[2]) { 
      CASE 1: 
       print "<td class='w3-theme-d5'>Advanced</td>\n"; 
       break; 
      CASE 2: 
       print "<td class='w3-theme-d5'>Intermediate</td>\n"; 
       break; 
      CASE 3: 
       print "<td class='w3-theme-d5'>Beginner</td>\n"; 
       break; 
      DEFAULT: 
       print "<td class='w3-theme-d5'>Beginner</td>\n"; 
       break; 
     } 
     $count++; 
    } while ($AA = mysqli_fetch_array($A)); 

} 
IF ($count < 5) { 
    do { 
     print "<tr><td colspan='3'>[Your name could be here]</td></tr>\n"; 
     $count++; 
    } while ($count < 5); 
} 
print "</table>\n"; 
} 

更新:我發現我的答案的部分,有一個工作的解決方案,是一個kludge,請參閱下面的答案。

+1

我會嘗試儘可能多地向數據庫查詢推送數據管理。您可以通過任何方式將簡化問題摘要添加到您的帖子中?我對實際問題是什麼感到困惑。 –

+0

有問題嗎? – Strawberry

+0

@JasonJoslin我可以試着簡化這個問題。我無法使用SQL查詢來構建HTML輸出。我需要能夠根據SQL查詢的結果安排來自PHP的六個顯示塊。 –

回答

0

在建議將新信息存儲到中間步驟後,我決定使用每小時cron-job生成的緩存頁面。這使我能夠像SQL查詢那樣複雜,因爲它只能每小時運行一次。這導致分配給$my_var1這個怪物SQL查詢:SQL Fiddle Link

SELECT 
    game, 
    average 
FROM 
    (
    SELECT 
     '1' AS game, 
     AVG(g1score) AS average 
    FROM 
     (
     SELECT 
      Game1Score AS g1score 
     FROM 
      game1 
    ) tmp1 
) tmp2 
UNION ALL 
    (
    SELECT 
     '2' AS game, 
     AVG(g2score) AS average 
    FROM 
     (
     SELECT 
      Game2Score AS g2score 
     FROM 
      game2 
    ) tmp3 
) 
UNION ALL 
    (
    SELECT 
     '3' AS game, 
     AVG(g3score) AS average 
    FROM 
     (
     SELECT 
      Game3Score AS g3score 
     FROM 
      game3 
    ) tmp4 
) 
UNION ALL 
    (
    SELECT 
     '4' AS game, 
     AVG(g4score) AS average 
    FROM 
     (
     SELECT 
      Game4Score AS g4score 
     FROM 
      game4 
    ) tmp5 
) 
UNION ALL 
    (
    SELECT 
     '5' AS game, 
     AVG(g5score) AS average 
    FROM 
     (
     SELECT 
      Game5Score AS g5score 
     FROM 
      game5 
    ) tmp7 
) 
UNION ALL 
    (
    SELECT 
     '6' AS game, 
     AVG(g6score) AS average 
    FROM 
     (
     SELECT 
      Game6Score AS g6score 
     FROM 
      game6 
    ) tmp8 
) 
UNION ALL 
    (
    SELECT 
     '7' AS game, 
     '999' AS average 
) 
ORDER BY 
    average 
DESC 

這將返回該數據集:

Screencap of PHPMyAdmin Query Results

這是我可以用一個簡單的開關/ CASE調用函數的格式包含Top5得分以所需順序顯示:

do { 
    SWITCH ($my_query_result) { 
     CASE 1: 
      t5_game_1($connection); 
      BREAK; 
     CASE 2: 
      t5_game_2($connection); 
      BREAK; 
     CASE 3: 
      t5_game_3($connection); 
      BREAK; 
     CASE 4: 
      t5_game_4($connection); 
      BREAK; 
     CASE 5: 
      t5_game_5($connection); 
      BREAK; 
     CASE 6: 
      t5_game_6($connection); 
      BREAK; 
    } 
} WHILE ($my_query_result = mysqli_fetch_array($my_var1)); 

(是的,缺少DEFAULT CASE或案例7是故意的)

事情是,由於一些非常奇怪的原因,這是不返回預期的結果,當在網上生活。我從來沒有想過問題是什麼,只有一個補丁讓它可靠地工作。也許你發現了怪物查詢的最後一個元素或結果的第一行中的修復?

結果的第一行不會被解析。無論頂級遊戲如何,它總是會下降到DEFAULT動作。因此,我添加了一個額外的異常值,以便始終領導結果集並被導致問題的任何東西所破壞。剩下的六行(如果沒有數據,則返回null)傳遞到SWITCH/CASE並生成六個聯盟前五名得分。

這是我通常認爲UGLY解決方案,但它的工作原理。雖然我希望我能弄清楚爲什麼結果集的第一行總是失敗,但其餘的行不行。

相關問題