Export Mysql to Excel with custom formatting

Rintaka Jayadi Brata · Apr 2, 2013
Dear suhu-suhu Sangkep sekalian..

Ada yang mau ditanyain nih..
Gw disuruh bikin export to excel dari mysql tapi lewat php dengan susunan tabel dan isi tabel seperti dibawah ini:

|| NIK || NAMA || tanggal awal || .... || ..... || tanggal akhir || Total ||
|| || ||absen| telat || ..|..||..|....|| absen | telat || ||
||A-1 || Abdi || 8.45| 00.15 || ..|..||..|....|| 9.00 | 00.30 || 02.00 ||


Gw udah kepikiran buat bikin kodingan kayak gini
<table align="center" border="1" cellpadding="5" cellspacing="0">
	<tr>
		<th rowspan=2><font size="2" face="Arial">NIK</font></th>
		<th rowspan=2><font size="2" face="Arial">Name</font></th>
		
<?php
date_default_timezone_set('Asia/Jakarta');
$begindate = '2013-03-25';
$enddate = '2013-03-29';

$q= "SELECT DISTINCT tanggal FROM absen WHERE tanggal BETWEEN '$begindate' AND'$enddate' AND DAYOFWEEK(tanggal)NOT IN(1,7)";
$query1 = mysql_query($q) or die(mysql_error());
$count = mysql_num_rows($query1);
while ($fetch = mysql_fetch_row($query1)){
		$tanggal = $fetch['0'];
		?><th colspan=2><font size="2" face="Arial"><?php echo $tanggal;?></font></th><?php 
}?>
	<th rowspan=2><font size="2" face="Arial">Total telat</font></th>
	</tr>
	<tr>
<?php
while ($count>0){
	echo '<th><font size="2" face="Arial">Masuk</font></th>';
	echo '<th><font size="2" face="Arial">Telat</font></th>';
	$count--;
}?>
	</tr>
<?php

$cquery = "SELECT nik,fname,lname FROM staff ORDER BY fname ASC";
$result = mysql_query($cquery) or die(mysql_error());

while($row1= mysql_fetch_array($result))
	{
	$nik = $row1['nik'];
	
	echo "<tr>";
	echo "<td><font size='2' face='Arial'>" . $row1['nik'] . "</font></td>";
	echo "<td><font size='2' face='Arial'>" . $row1['fname']." ".$row1['lname']. "</font></td>";
	
	$q2 = "SELECT checkin, telat2, checkout,ket,rank FROM absen WHERE nik = '$nik' AND tanggal BETWEEN '$begindate' AND '$enddate' AND DAYOFWEEK(tanggal)NOT IN(1,7) ";
	$result1 = mysql_query($q2) or die(mysql_error());
		
	while($row2 = mysql_fetch_row($result1)){
		$checkin = $row2['0'];
		$telat2 = $row2['1'];
		$checkout = $row2['2'];
		$ket = $row2['3'];
		$rank = $row2['4'];
		
		if (empty($rank) && isset($ket) ){
			echo "<td><font size='2' face='Arial'> </font></td>";
			echo "<td><font size='2' face='Arial'> </font></td>";}
		elseif(($checkout=='00:00:00'&&$checkin=='00:00:00')){
			echo "<td><font size='2' face='Arial'> </font></td>";
			echo "<td><font size='2' face='Arial'> </font></td>";}
		elseif(empty($rank)){}
		elseif(empty($ket)&&$rank !== ''){
			echo "<td><font size='2' face='Arial'>" . date('H:i', strtotime($row2['0'])) . "</font></td>";
			echo "<td><font size='2' face='Arial'>" . $row2['1'] . "</font></td>";}
			}}?>
	 </tr>
</table>

Tapi ada masalah lain yang muncul, satu NIK dalam satu hari bisa absen berkali2, nah gimana caranya gw mensortir itu dan hanya memasukkan record yang pertama saja..

Terima kasih sebelumnya...
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Apr 4, 2013 · 0 Suka · 0 Tidak Suka
bisa liat struktur tablenya?
0
Loading...
Rintaka Jayadi Brata · Apr 5, 2013 · 0 Suka · 0 Tidak Suka
Struktur tabel nya yah?
1. log_absen || int(6)
2. rank || varchar(5)
3. nik || varchar(15)
4. fname || varchar(255)
5. lname || varchar(255)
6. lokasi || varchar(30)
7. tanggal || date
8. waktu || time
9. telat || varchar(255)
10.telat2 || varchar(255)
11.checkout || time
12.tujuan || varchar(255)
13.status || varchar(20)
14.ket || varchar(255)