你的HTML代碼似乎很好,但SQL查詢沒有。我假設你在問如何做出正確的查詢。在使用GROUP BY時,您應該(並且在默認情況下,在較新的MySQL版本中必須)僅選擇具有SUM,AVG等聚合函數的字段,或選擇在GROUP BY中指定的字段。您無法通過選擇的結果進行分組。
您的查詢應該是這樣的:
SELECT DATE(waktu) Tgl FROM absen WHERE id_peg=1 GROUP BY DATE(waktu)
編輯:好了,根據谷歌翻譯和其他網站目錄注入/ Keluar是由於/截止。並使用新提供的信息,這應該做的伎倆:直到遇到另一個日期
<center>
<table border=1 cellpadding=10>
<tr>
<td rowspan="2">TANGGAL</td>
<td colspan="2" align="center">PAGI</td>
<td colspan="2" align="center">SORE</td>
<td rowspan="2">JML. JAM</td>
<td rowspan="2">JML. Rp</td>
</tr>
<tr>
<td>Masuk</td>
<td>Keluar</td>
<td>Masuk</td>
<td>Keluar</td>
</tr>
<?php
function display_row($timeRanges) {
$pagi = isset($timeRanges[1]) ? strtotime($timeRanges[1]) - strtotime($timeRanges[0]) : 0;
$sore = isset($timeRanges[3]) ? strtotime($timeRanges[3]) - strtotime($timeRanges[2]) : 0;
$seconds = $pagi + $sore;
echo "<tr>";
echo "<td>" . substr($timeRanges[0], 0, 10) . "</td>"; // TANGGAL
echo "<td>" . substr($timeRanges[0], 11, 8) . "</td>"; // PAGI Masuk
echo "<td>" . (isset($timeRanges[1]) ? substr($timeRanges[1], 11, 8) : "") . "</td>"; // PAGI Keluar
echo "<td>" . (isset($timeRanges[2]) ? substr($timeRanges[2], 11, 8) : "") . "</td>"; // SORE Masuk
echo "<td>" . (isset($timeRanges[3]) ? substr($timeRanges[3], 11, 8) : "") . "</td>"; // SORE Keluar
echo "<td>" . round($seconds/3600, 2) . "</td>"; // JML. JAM shows rounded number of hours
echo "<td>" . round($seconds * 5000/3600) . "</td>"; // JML. Rp number hours * 5000
echo "</tr>";
}
$qry_tgl=mysql_query("SELECT DATE(waktu) Tgl, waktu FROM absen WHERE id_peg=1 ORDER BY waktu");
$lastDate = null;
$timeRanges = array();
while($row=mysql_fetch_array($qry_tgl)){
if($row['Tgl'] !== $lastDate) {
if($lastDate !== null)
display_row($timeRanges); // renders the row when the date changes, but only if we fetched at least one date
$lastDate = $row['Tgl'];
$timeRanges = array();
}
$timeRanges[] = $row["waktu"];
}
if($lastDate !== null)
display_row($timeRanges); // renders the last row when the loop ends, but also only if we fetched at least one date
echo "</table></center>";
該算法讀取日期,$timeRanges
陣列。因此,當調用display_row()
函數時,$timeRanges
將僅包含相同日期的有序記錄。
恐怕只用MySQL來做會很慢,而且會浪費資源。
*「我想在PHP中顯示錶格,就像這個HTML php表格」* - 問題是;你現在得到什麼結果?編輯:爲了有一些東西可以比較。 –
你能看到紅色的盒子嗎? http://php.net/manual/en/function.mysql-query.php – Andreas
@Andreas對不起,這是很久以前:) – Jens