2014-04-10 53 views
0

我想讓PHP給我一些統計數據在我的參與者列表中。我需要知道每個聯盟有多少人每個聯盟(從) 如果您不知道所有聯盟和職位,有沒有簡單的方法可以做到這一點?Php數據庫信息排序

<?php 
include 'includes/config.php'; 

$sql="SELECT * FROM participants ORDER BY `from`, `status`"; 

$self = mysql_query($sql); 

echo "<table><tr><th>Name</th><th>Affiliation</th><th>Status</th></tr>"; 

while ($row = mysql_fetch_array($self)) { 
    echo "<tr><td>".$row['name']."</td><td>".$row['from']."</td><td>".$row['status']."</td></tr>"; 
} 
echo "</table>"; 
?> 

數據庫的結構是 Name - Affiliation - Status

所以大家誰寄存器將被放置在像 Bob - Minnesota University - Phd Student

現在我希望所有的從屬關係的列表,每個聯繫我需要一個列表所有的stati和多少人分享他們。

所以像: Minnesota University Phd Student: 5 Professor: 12 Bachelor Student: 4 Caltech Phd Student: 1 Professor: 14 Bachelor Student:32

+3

你能澄清更好一點,你需要什麼?也許提供你的表格結構和樣本數據? –

+0

如果您設置了[SQL小提琴](http://sqlfiddle.com/) –

+0

,那麼我們可以幫助您更輕鬆地發佈includes/config.php文件嗎? – Daan

回答

0

這可能是這樣:

$sql="SELECT count(*) as total_status, * FROM participants 
    GROUP BY `status`, `from` ORDER BY `from`"; 

    $self = mysql_query($sql); 

    echo "<table> 
      <tr> 
      <th>Name</th> 
      <th>Affiliation</th> 
      <th>Status</th> 
      <th>Total Status</th> 
      </tr>"; 

    while ($row = mysql_fetch_array($self)) { 
     echo "<tr> 
       <td>".$row['name']."</td> 
       <td>".$row['from']."</td> 
       <td>".$row['status']."</td> 
       <td>".$row['total_status']."</td> 
      </tr>"; 
    } 
    echo "</table>"; 
+0

它似乎不起作用。但從我能告訴它試圖找到每個名字的聯盟數量? 姓名是與某事物有聯繫並且也有身份的參與者的姓名。所以我想列出所有的聯盟,看看他們有多少個地位。 – Coolcrab

+0

它給出了該聯盟的狀態,狀態和計數, 將其更改爲「SELECT count(*)as total_status,* FROM participants GROUP BY'status','from' ORDER BY'from'」; – Vitthal