2017-06-01 38 views
-3

我有這個表在MySQL如何顯示錶行這樣

enter image description here

這是mycode的 //生成報告

<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 
    $qry_tgl=mysql_query("SELECT id_peg, date(waktu) Tgl from absen where id_peg=1 group by Tgl"); 
    while($row=mysql_fetch_array($qry_tgl)){  
     echo "<tr>"; 
      echo "<td>".$row['Tgl']."</td>"; 
     echo "</tr>"; 
    }   
    echo "</table></center>"; 

我想在PHP中顯示的表像這樣enter image description here

請幫幫我,我很困惑怎麼辦

謝謝你

+0

*「我想在PHP中顯示錶格,就像這個HTML php表格」* - 問題是;你現在得到什麼結果?編輯:爲了有一些東西可以比較。 –

+1

你能看到紅色的盒子嗎? http://php.net/manual/en/function.mysql-query.php – Andreas

+0

@Andreas對不起,這是很久以前:) – Jens

回答

1

你的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來做會很慢,而且會浪費資源。

+1

抱歉,但它不完全是我想要的。我想用mysql waktu列的查詢結果來填充空格列表(見html表格)。例如Mysql waktu列具有像2017-06-01一樣的重複值4次(請參閱mysql表),這是我想填充到空白html表空間。 html表格2次(痠痛:masuk,keluar)2次。對不起我的英語不好。謝謝 –

+0

@KomangPanca,什麼是JML.JAM和JML.Rp列?會有來自同一張表或其他表格的任何信息嗎?如果是的話那麼什麼信息?我需要知道,因爲它會影響我將如何編寫最終算法。 – Viacheslav

+1

謝謝你的快速回復。 JML。 JAM是時間列Pagi的總和計算:(Keluar( - )Masuk)(+)Sore:(Keluar( - )Masuk)。而JML Rp是JML的計算。 JAM * 24 * 5000(5000小時工資) –

相關問題