假设我有一个名为“farmstuff”的表,就像这个高度简化且看起来很奇怪的表;
id mark pjob role jobDesc 1 1 horses Smithy Smith Stuff 1 1 horses Smithy More Smith stuff 1 1 horses Smithy Even More Smith stuff 2 1 cows Farrier Put left front shoe on 2 1 cows Farrier Put right front shoe on
我的PHP阅读它是这样的:
<?php
$tableheader = "<table><thead><tr><th>col3</th><th>col4</th></tr></thead><tbody>";
$sql = "SELECT id, mark, pjob, role, jobDesc FROM farmstuff WHERE mark = 1 ORDER BY id";
$echo($tableheader);
foreach($found->query($sql) as $row) {
echo ("<tr>");
}
echo("<td>$row[role]</td>");
echo("<td>$row[jobDesc]</td>");
} echo("</tr></tbody></table>");
?>
我想要的是两张如下表。尽管其他变量的值,我想在id的值的变化打破表。因此,如果它的1然后第一个标题是马,如果它是2那么标题是奶牛。请记住这些是现在的表格。首先,我如何从SQL的输出中创建两个具有适当中断的表,然后如何将标签放在每个表的顶部,如下例所示?
horses col4 col5 Smithy Smith Stuff Smithty More Smith Stuff Smithy Even More Smith Stuff cows col4 col5 Farrier Put left front shoe on Farrier Put right front shoe on
答案 0 :(得分:1)
这未经过测试(更多原型),但有类似的内容:
<?php
$tableheader = "<table><thead><tr><th>col3</th><th>col4</th></tr></thead><tbody>";
$sql = "SELECT id, mark, pjob, role, jobDesc FROM farmstuff WHERE mark = 1 ORDER BY id";
$echo($tableheader);
$lastid = '';
foreach($found->query($sql) as $row) {
if($lastid != $row[role]) {
echo ("<tr><td colspan=2>$row[id]</td></tr>");
}
echo ("<tr>");
echo("<td>$row[role]</td>");
echo("<td>$row[jobDesc]</td>");
echo("</tr>");
$lastid = $row[role];
}
echo("</tbody></table>");
?>