【问题标题】:PHP/MySql HTML Table GroupingPHP/MySql HTML 表格分组
【发布时间】:2013-08-19 07:13:31
【问题描述】:

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

我有一个类似这样的 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 1 个 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

老实说,我被困住了...... 提前感谢您的帮助!

【问题讨论】:

    标签: php mysql sql html-table


    【解决方案1】:
    $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>';
    

    【讨论】:

    • 无论第一个 ID 是否有更多条目,这个循环在第一次迭代时是否会开始和结束表格?
    • 当我使用你的代码时,我仍然得到一个唯一的表(虽然我确实必须进行 1 次更改,在 $id != $row['userid']; 行中添加了一个 ;有没有办法将每个唯一用户 ID 合并到一个表中?感谢您的时间/帮助!
    • @kevin 我认为他在那一行打错了。将 != 更改为 = ,它应该可以正常工作。 $id != $row['userid'] 应该是 $id = $row['userid'];
    【解决方案2】:

    这样的?

    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>';
    

    【讨论】:

      【解决方案3】:

      你需要这样的东西:

      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>';
      

      【讨论】:

        猜你喜欢
        • 2011-09-24
        • 1970-01-01
        • 2016-01-29
        • 2014-08-22
        • 1970-01-01
        • 2016-04-12
        • 2012-01-16
        • 1970-01-01
        • 2014-07-08
        相关资源
        最近更新 更多