【问题标题】:rowspan in PHP while loop with a lot of dataPHP while 循环中包含大量数据的行跨度
【发布时间】:2015-10-21 07:38:57
【问题描述】:

我目前正在处理我的桌子,并根据客户的房间 ID 对结果进行分组。我通过我的 sql 查询来做到这一点。

The query can be found here.

棘手的部分是自动添加rowspan 来安排输出。目前,我的表格将我的结果显示为;

+------------------------------------------------------------+
| rID    UID    Name    Surname   Room Type   Total    Paid  |
+------------------------------------------------------------+
| 1035   1010   John    Doe        double      200      200  |
| 1035   1011   Jane    Doe        double      200      200  |
| 1036   1012   Fred    Nerk       single      150      150  |
| 1037   1013   Adam    McKenzie   double      200      200  |
| 1037   1014   Rose    McKenzie   double      200      100  |
+------------------------------------------------------------+
* keep in mind this is only an example - normally there are 24 columns.

虽然我想要的表格输出是;

+------------------------------------------------------------+
| rID    UID    Name    Surname   Room Type   Total    Paid  |
+------------------------------------------------------------+
| 1035   1010   John    Doe        double      200      200  |
|        1011   Jane    Doe        double      200      200  |
| 1036   1012   Fred    Nerk       single      150      150  |
| 1037   1013   Adam    McKenzie   double      200      200  |
|        1014   Rose    McKenzie   double      200      100  |
+------------------------------------------------------------+
* keep in mind this is only an example - normally there are 24 columns.

我找到了一些解决方案,但唯一的问题是,在所有的解释和问题中,只有两个变量需要从数据库中调用,这让我感到困惑。例如,This answer 在我将所有变量输入数组并将它们实现到代码中时起作用。虽然现在我有 24 个数组,但它确实阻塞了我的代码。有没有更有效的解决方案?

目前,将所有结果输入数组,我的代码如下所示;

$arr = array();

        $a_UID = array(); $a_pdID = array(); $a_name = array(); $a_surname = array(); $a_address = array(); $a_city = array(); $a_ci = array(); $a_co = array();
        $a_transfer = array(); $a_confirm = array(); $a_membership = array(); $a_rtype = array(); $a_notes = array(); $a_entryby = array(); $a_dateentered = array();
        $a_updateby = array(); $a_updatetime = array(); $a_total = array(); $a_paid = array(); $a_pmethod = array(); $a_currency = array(); $a_auth = array(); $a_invoice = array();

        // rID array
        $emp = array();

        while( $guests->fetch() ) {
            array_push($emp, $rID);
            array_push($a_UID, $UID);
            array_push($a_pdID, $pdID);
            array_push($a_name, $name);
            array_push($a_surname, $surname);
            array_push($a_address, $address);
            array_push($a_city, $city);
            array_push($a_ci, $ci);
            array_push($a_co, $co);
            array_push($a_transfer, $transfer);
            array_push($a_confirm, $confirm);
            array_push($a_membership, $membership);
            array_push($a_rtype, $rtype);
            array_push($a_notes, $notes);
            array_push($a_entryby, $entryby);
            array_push($a_dateentered, $dateentered);
            array_push($a_updateby, $updateby);
            array_push($a_updatetime, $updatetime);
            array_push($a_total, $total);
            array_push($a_paid, $paid);
            array_push($a_pmethod, $pmethod);
            array_push($a_currency, $currency);
            array_push($a_auth, $auth);
            array_push($a_invoice, $invoice);

            >which really does not look that good.

【问题讨论】:

  • 遍历您的结果集,并且对于每条记录,如果它不同于您之前阅读的记录,则只显示rID(当然总是显示rID第一条记录。为什么这很难实现?
  • 请提供您的查询给您的确切输出??
  • 实现并不是最困难的部分,我真的不觉得在一个页面中有超过 25 组数组写了 3 次,这很吸引眼球。因此它破坏了代码的溢出,我也相信它会影响性能,因为我有时会调用 1000 条记录。因此,为什么我要问是否有办法只搜索 rID 并留下其余的 @Priyank57,确切的输出显示在第一个示例中 - 当然变量更少。

标签: php mysql arrays html-table


【解决方案1】:

我要解决这个问题的方法是从分组字段中创建一个键控数组...我假设您正在从已执行的语句中获取数据作为关联数组。

$res = array();
while ($rec = $dbStmt->fetch()){
    if (!isset($res[ $rec["rID"] ])) $res[ $rec["rID"] ] = array();
    $res[ $rec["rID"] ][] = array_slice($rec, 1);
}

现在可以轻松计算您有多少条记录共享行跨度的公共 rID。然后就是按照你想要的格式输出表格了。

echo "<table>";
foreach ($res as $rID=>$other){
    $rID = '<td rowspan="', count($other), '">', $rID, '</td>';
    array_shift($other);
    foreach ($order as $otherFields){
        echo "<tr>", $rId, "<td>".implode("</td><td>", $otherFields)."</td></tr>";
        $rId = "";
    }
}
echo "</table>"; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2014-02-07
    • 1970-01-01
    • 2017-10-08
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多