2015-05-25 63 views
-2

我正在爲一些好友開發一些基本的統計跟蹤網站,並希望更多地組織單個玩家頁面。sql group by - 顯示所有行

我的計劃是根據他們玩的日期打印出不同的表格,這樣他們就可以更輕鬆地看到特定日期的遊戲。

我很新的PHP和發現了「GROUP BY」,雖然這正確地按日期分組,它只返回一行...有沒有辦法讓「GROUP BY?」中的每一行。

繼承人什麼我現在有:

$query = "SELECT * FROM games WHERE player='$thePlayer' GROUP BY whenPlayed"; 
+0

你應該使用'ORDER BY'代替'GROUP BY',例如'SELECT * FROM games WHERE player ='$ thePlayer'ORDER BY whenPlayed ASC' – Populus

+0

這是我原來的計劃,但是,我一直在爲分開的日期打印單獨的表格打印邏輯而苦苦掙扎。 – Nihat

+0

跟蹤當前日期... – jeroen

回答

0
// Get data 
$query = "SELECT * FROM games WHERE player = '$thePlayer' ORDER BY whenPlayed"; 
$result = $mysqli->query($query); 


$data = array(); 
// Iterate over data, use 'whenPlayed` as a key to group your data 
while ($row = $result->fetch_array(MYSQLI_ASSOC)) { 
    if (!array_key_exists($row['whenPlayed'], $data)) { 
     $data[$row['whenPlayed']] = array(); 
    } 
    $data[$row['whenPlayed']][] = $row; 
} 

然後在列表中顯示出來,例如:

<ul> 
    <?php foreach ($data as $date => $games): ?> 
    <li> 
     <?php echo $date ?>: 
     <ul> 
      <?php foreach ($games as $game): ?> 
      <li><?php echo print_r($game, true) ?></li> 
      <?php endforeach ?> 
     </ul> 
    </li> 
    <?php endforeach ?> 
</ul> 
+0

非常好,謝謝。 – Nihat

0

試試這個辦法第一ORDER你的行爲whenPlayed升序或降序排列,並添加where條件,如果你需要對個別日期或其他東西排插槽。

$query = "SELECT * FROM games WHERE player='$thePlayer' ORDER BY whenPlayed ASC";