1 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
我正在更新我的答案,因?yàn)樗蛭粗蚨唤导?jí),沒(méi)有人會(huì)解釋。
所以這是我的回答,基于我所做的和基于邏輯的。
簡(jiǎn)而言之,不要嘗試在 PHP 中做你想做的事情,因?yàn)樗鼤?huì)在你的循環(huán)代碼中創(chuàng)建太多額外的工作和復(fù)雜性,否則你需要進(jìn)行預(yù)先計(jì)算以知道要為數(shù)量設(shè)置的行跨度該期間的項(xiàng)目在輸出該期間的項(xiàng)目之前。
在我繼續(xù)之前,不妨問(wèn)自己一個(gè)問(wèn)題:“為什么我只需要在周期發(fā)生變化時(shí)打印一次,這對(duì)最終用戶有什么幫助?如果有的話?”
由于您仍在使用列,因此嘗試按句點(diǎn)對(duì)它們進(jìn)行分組并沒(méi)有節(jié)省任何空間,因此您可以在頁(yè)面的下方一直重復(fù)該列中的句點(diǎn),因?yàn)槿绻腥讼蛳聺L動(dòng)并且句點(diǎn)不在左側(cè)他們可能需要向上滾動(dòng)才能記住他們所處的時(shí)期。
如果您想避免 PHP 預(yù)先計(jì)算并且仍然做您想做的事情,您可以僅在您知道它已更改時(shí)打印句點(diǎn),然后使用 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 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)