我试图在PHP中回显一个表,其中约会列表显示如下。
-----------------------------------
Date | Company | Executive Name
-----------------------------------
01 | Comapany1 | Mr. XX1
-----------------------------------
01 | Comapany2 | Mr. XX2
-----------------------------------
02 | Comapany3 | Mr. XX3
-----------------------------------
02 | Comapany4 | Mr. XX4
-----------------------------------
02 | Comapany5 | Mr. XX5
-----------------------------------
03 | Comapany6 | Mr. XX6
-----------------------------------
但是我想这样回声.......这里的rowspan是可变的。因为约会的数量因日期而异:
-----------------------------------
Date | Company | Executive Name
-----------------------------------
01 | Comapany1 | Mr. XX1
----------------------------
| Comapany2 | Mr. XX2
-----------------------------------
| Comapany3 | Mr. XX3
----------------------------
02 | Comapany4 | Mr. XX4
----------------------------
| Comapany5 | Mr. XX5
-----------------------------------
03 | Comapany6 | Mr. XX6
-----------------------------------
我怎样才能做到这一点......以下是我目前的代码
while(($result = mysql_fetch_assoc($query)) !== false) {
echo "<tr>";
echo"<td width=15%>" . $app_date."</td>";
echo"<td align=left> " . $result['company'] . "</td>";
echo"<td align=left> " . $result['exe_name'] . "</td>";
echo"<td>" . $result['mobile'] . "</td>";
echo "</tr>";
答案 0 :(得分:0)
这是完全有效的代码:
$groupedRows = array();
$results = array(array('app_date' => '2012-03-01', 'company' => 'abc' , 'exe_name' => 'lol'),
array('app_date' => '2012-03-01', 'company' => 'def' , 'exe_name' => 'lol2'),
array('app_date' => '2012-03-03', 'company' => 'lololololol' , 'exe_name' => 'lol4')
);
foreach ($results as $result) {
$groupBy = $result['app_date']; // this is simbolic - i don't know where do you get it from;
$groupedRows[$groupBy][] = array(
'company' => $result['company'], 'exe_name' => $result['exe_name']
);
}
$displayed = array();
foreach ($groupedRows as $groupedBy => $group) {
$rowSpan = count($group);
foreach ($group as $result) {
echo "<tr>";
if (empty($displayed[$groupedBy])){
$displayed[$groupedBy] = true;
echo"<td rowspan='".$rowSpan."' width=15%>" . $groupedBy."</td>";
}
echo"<td align=left> " . $result['company'] . "</td>";
echo"<td align=left> " . $result['exe_name'] . "</td>";
echo"<td>" . $result['mobile'] . "</td>";
echo "</tr>";
}
}