我目前正在处理我的桌子,并根据客户的房间ID对结果进行分组。我通过我的SQL查询来做到这一点。
棘手的部分是安排自动添加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.
答案 0 :(得分:0)
我将这样做的方法是创建一个键控字段的数组...我假设你从一个执行语句中获取数据作为一个关联数组。
$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>";