如何使用PHP循环在单独的表中显示分组数据? [英] How to display grouped data in separate tables with a PHP loop?

查看:92
本文介绍了如何使用PHP循环在单独的表中显示分组数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是phpmysqli的新手.这是我所拥有的以及正在努力实现的目标:我将根据建议进行更新; 数据库样本数据

I am new to phpmysqli. Here is what I have and what am trying to achieve: I will update this based on the recommendations; Database sample data

我想根据每个学生的sid在一页上显示数据,并为每个学生提供单独的表格.到目前为止,这是我尝试过的;

I want to display the data on one page with separate tables for each student based on their sid. This is what I have tried so far;

<?php
include_once 'dbcon.php';

$results = $MySQLiconn->query('SELECT * FROM activitybook');

$students = [];

foreach ( $results->fetch_array() as $activity ) {
    $students[$activity['sid']][] = $activity;
}

foreach($students as $sid=>$activities) {
    foreach($activities as $activity) {
         echo
                    "<table><tr>
                        <th>SID</th>
                        <th>Date</th>
                        <th>FName</th>
                        <th>LName</th>
                        <th>activity</th>
                        <th>time</th>
                        <th>score</th>
                        </tr>
                <tr>
                    <td>" . $sid . "</td>
                    <td>" . $activity['fname'] . "</td>
                    <td>" . $activity['lname'] . "</td>
                    <td>" . $activity['activity'] .  "</td>
                    <td>" . $activity['atime'] .  "</td>
                    <td>" . $activity['ascore'] .  "</td>
                </tr></table>";
    }
}
?>

这就是我所得到的

试图实现的是为每个sid提供单独的表. 这是我要存档的示例

What am trying to achieve is separate tables for each sid. This is the sample of what I want to archive

推荐答案

您将需要根据sid值对结果集数据进行分组".进行迭代时,请检查是否要更改组.

You will need to "group" your result set data based on the sid value. As you iterate, check if you are changing groups or not.

我也添加了一些改进.

  • 命名SELECT子句中的列,以便仅精确检索所需的内容.
  • 获取关联数组,而不是索引元素和关联元素的组合.
  • 分配一个临时变量以帮助您确定是继续sid组还是开始新的组(或者如果是第一次迭代,则不要写</table>.
  • implode()有助于消除很多代码膨胀.
  • Name the columns in your SELECT clause so that you only retrieve exactly what you need.
  • Fetch an associative array, not a combination of indexed and associative elements.
  • Assign a temporary variable to help you determine if you are continuing a sid group or starting a new one (or if it is the first iteration, don't write </table>.
  • implode() helps to remove a lot of the code bloat.

代码:

$res = $conn->query("SELECT sid, fname, lname, activity, atime, ascore FROM activitybook ORDER BY sid");
$tmp = null;
$colheads = ['SID', 'FName', 'LName', 'activity', 'time', 'score'];
while ($row = $res->fetch_assoc()) {   // don't produce more elements per row than needed
    if ($tmp != $row['sid']) {  // determine if a new group / sid value
        if ($tmp !== null) {
            echo '</table><br>';  // close the previous table
        }
        echo '<table border=1><tr><th>' , implode('</th><th>', $colheads) , '</th></tr>';  // start a new table & add headings
    }
    echo '<tr><td>' , implode('</td><td>', $row) , '</td></tr>';  // display the row data
    $tmp = $row['sid'];   // DUH, I FORGOT TO UPDATE $tmp!
}
if ($tmp !== null) {
    echo '</table>';  // close the final table -- so long as there was at least one row in the result set
}

这篇关于如何使用PHP循环在单独的表中显示分组数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆