将 MYSQL 结果分成单独的 HTML 表 [英] Separate MYSQL results into separate HTML Tables

查看:34
本文介绍了将 MYSQL 结果分成单独的 HTML 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在每个组之后制作单独的 HTML 表格:

I have the following in which I am trying to make separate HTML tables after each group:

$query = "SELECT * FROM NonBillableTime WHERE TimeIn LIKE '$MondayP2%' ORDER BY CreatedUser ASC";
$members = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while ($row = mysql_fetch_assoc($members)) {

示例HTML 表 1 将返回 CreatedUser 编号为 5 的所有内容然后该表将结束,一个新表将开始并返回 CreatedUser 编号为 6 的所有内容.

我面临两个问题:-

  1. 我不知道自从它发生变化后我有多少用户.
  2. 它需要从 while ($row = mysql_fetch_assoc($members)) { 执行,因为我正在执行其他计算
  1. I don't know ahead how many users I have since it changes.
  2. It needs to execute from while ($row = mysql_fetch_assoc($members)) { since I am doing other calculations on the fly

是的,我想使用 MYSQL 并且知道添加预防注入.

推荐答案

这里的代码非常通用,但大概你目前正在做这样的事情:

Keeping the code pretty generic here, but presumably you're currently doing something like this:

// output a table header
while ($row = mysql_fetch_assoc($members)) {
    // output a table row
}
// output a table footer

如果您想在该循环中定期开始一个新表,您需要添加一个条件来确定何时执行此操作.所以结构会更像这样:

If you want to begin a new table periodically in that loop, you'd need to add a condition to determine when to do that. So the structure would be more like this:

$currentUser = 1;
// output a table header
while ($row = mysql_fetch_assoc($members)) {
    // output a table row
    if ($row["CurrentUser"] != $currentUser) {
        // output a table footer
        // output a table header
        $currentUser = $row["CurrentUser"];
    }
}
// output a table footer

这是非常现成的,因此这里可能存在逻辑错误,无可否认,在某些条件下或类似性质的情况下显示部分表格.但希望这个想法的要点正在被传达.基本上在循环内,您可以根据条件关闭并重新打开表格(将您拥有的数据中的任何信息放入这些页眉/页脚).您只需要跟踪在该条件下使用的数据.在这种情况下,结果的当前"CurrentUser 值.

This is pretty off-the-cuff, so there may be a logical mistake in here by which a partial table is displayed under certain conditions or something of that nature, admittedly. But hopefully the gist of the idea is being conveyed. Essentially within the loop you can close and re-open the table (putting whatever information from the data you have into those headers/footers) based on a condition. You just have to track the data being used in that condition. In this case, the "current" CurrentUser value of the results.

这篇关于将 MYSQL 结果分成单独的 HTML 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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