1 回答

TA贡献1864条经验 获得超6个赞
我正在更新我的答案,因为它因未知原因而被降级,没有人会解释。
所以这是我的回答,基于我所做的和基于逻辑的。
简而言之,不要尝试在 PHP 中做你想做的事情,因为它会在你的循环代码中创建太多额外的工作和复杂性,否则你需要进行预先计算以知道要为数量设置的行跨度该期间的项目在输出该期间的项目之前。
在我继续之前,不妨问自己一个问题:“为什么我只需要在周期发生变化时打印一次,这对最终用户有什么帮助?如果有的话?”
由于您仍在使用列,因此尝试按句点对它们进行分组并没有节省任何空间,因此您可以在页面的下方一直重复该列中的句点,因为如果有人向下滚动并且句点不在左侧他们可能需要向上滚动才能记住他们所处的时期。
如果您想避免 PHP 预先计算并且仍然做您想做的事情,您可以仅在您知道它已更改时打印句点,然后使用 CSS 像这样格式化表格。
例如:
$lastPeriod="";
while($row = $mysqli->fetchassoc($res)){
echo "<tr>";
if($lastPeriod == $row["period"]){
echo "<td> </td>";
} else {
echo "<td class='period'>$row[period]</td>";
}
// echo the rest of the cells in the row
echo "</tr>";
$lastPeriod = $row["period"];
}
table{
border-collapse:collapse;
}
table tr th,
table tr td{
padding:5px;
border-color:#dddddd; /* Only have to set colour once this way*/
border-style:solid;
border-width:0px 0px 1px 0px;
}
table tr th{
background-color:#eeeeee;
}
table tr td:first-child{
border-width:0px;
}
table tr td.period{
border-width:0px 0px 1px 0px !important;
font-weight:bold;
}
<table>
<tr>
<th>Period</th>
<th>Name</th>
<th>Day</th>
<th>Options</th>
</tr>
<tr>
<td class='period'>September</td>
<td>John</td>
<td>01</td>
<td>-</td>
</tr>
<tr>
<td></td>
<td>Mary</td>
<td>05</td>
<td>-</td>
</tr>
<tr>
<td></td>
<td>Joe</td>
<td>06</td>
<td>-</td>
</tr>
<tr>
<td class='period'>October</td>
<td>John</td>
<td>22</td>
<td>-</td>
</tr>
</table>
- 1 回答
- 0 关注
- 167 浏览
添加回答
举报