PHP/MySql HTML Table Grouping


PHP/MySql HTML Table Grouping

我有一个问题,似乎无法解决。

我有一个类似于以下的mySQL表设置:

userid          date          rating1        rating2
1               5/1/2013      5              5
2               5/1/2013      4              4
3               5/1/2013      3              3
2               5/7/2013      5              5
1               5/7/2013      5              5
3               5/7/2013      2              2

我有我的php代码来查询数据:

$con=mysqli_connect("localhost","root","password","db");
$result = mysqli_query($con,"SELECT userid,date,rating1,rating2 FROM db");

但是当我尝试使用输出到一个表时

while($row = mysqli_fetch_array($result))
  {
echo '<table>';
    echo '<thead>';
    echo '<tr>';
        echo '<th>UserID</th>';
        echo '<th>Date</th>';
        echo '<th>First Rating</th>';
        echo '<th>Second Rating</th>';
            echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    echo '<tr>';
        echo '<td>'.$row['userid'].'</td>';
        echo '<td>'.$row['date'].'</td>';
        echo '<td>'.$row['rating1'].'</td>';
        echo '<td>'.$row['rating2'].'</td>';
            echo '</tr>';
    echo '</tbody>';
 echo '</table>';
  }

它为每个条目输出一个唯一的表。我需要的是每个UserID一个HTML表例如:

UserID 1
Date          First Rating        Second Rating
5/1/2013      5                   5
5/7/2013      5                   5

UserID 2
Date          First Rating        Second Rating
5/1/2013      4                   4
5/7/2013      5                   5

UserID 3
Date          First Rating        Second Rating
5/1/2013      3                   3
5/7/2013      2                   2

我真的被卡住了。。。提前感谢您的帮助!

$con=mysqli_connect("localhost","root","password","db");
$result = mysqli_query($con,"SELECT userid,date,rating1,rating2 FROM db ORDER BY userid");
$id = -1;
while($row = mysqli_fetch_array($result)) {
    if ($id != -1 && $id != $row['userid']) {
        echo '</tbody>';
        echo '</table>';
    }
    if ($id != $row['userid']) {
        echo '<table>';
        echo '<thead>';
        echo '<tr>';
        echo '<th>UserID</th>';
        echo '<th>Date</th>';
        echo '<th>First Rating</th>';
        echo '<th>Second Rating</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
        $id = $row['userid'];
    }
    echo '<tr>';
    echo '<td>'.$row['userid'].'</td>';
    echo '<td>'.$row['date'].'</td>';
    echo '<td>'.$row['rating1'].'</td>';
    echo '<td>'.$row['rating2'].'</td>';
    echo '</tr>';
 }
echo '</tbody>';
echo '</table>';

您需要类似以下内容:

echo '<table>';
echo '<thead>';
echo '<tr>';
    echo '<th>UserID</th>';
    echo '<th>Date</th>';
    echo '<th>First Rating</th>';
    echo '<th>Second Rating</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$old_id = 0;
while($row = mysqli_fetch_array($result))
{
    if( $old_id != $row['userid'] ) {
          echo "<tr><td colspan=3>".$row['userid']."</td>";
          $old_id = $row['userid'];
    } else {
        echo "<tr>";
        echo '<td>'.$row['userid'].'</td>';
        echo '<td>'.$row['date'].'</td>';
        echo '<td>'.$row['rating1'].'</td>';
        echo '<td>'.$row['rating2'].'</td>';
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';

这样的东西?

echo '<table>';
echo '<thead>';
echo '<tr>';
    echo '<th>UserID</th>';
    echo '<th>Date</th>';
    echo '<th>First Rating</th>';
    echo '<th>Second Rating</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = mysqli_fetch_array($result))
{
    echo '<tr>';
        echo '<td>'.$row['userid'].'</td>';
        echo '<td>'.$row['date'].'</td>';
        echo '<td>'.$row['rating1'].'</td>';
        echo '<td>'.$row['rating2'].'</td>';
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';