Advertisement
cdsatrian

presensi

Oct 15th, 2014
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. /*
  3. USE test;
  4. CREATE TABLE IF NOT EXISTS tb_karyawan(
  5.   nik VARCHAR(10) NOT NULL PRIMARY KEY,
  6.   nama VARCHAR(30) NOT NULL
  7. ) ENGINE=MyISAM;
  8.  
  9. INSERT INTO tb_karyawan
  10. VALUES
  11.   ('123456789','Dika Nanda'),
  12.   ('1234567890','Andika Dwi Chahya'),
  13.   ('2147483647','Akbar Alifian');
  14.  
  15. CREATE TABLE IF NOT EXISTS tb_presensi(
  16.   nik VARCHAR(10) NOT NULL,
  17.   tanggal DATE,
  18.   jam_masuk TIME,
  19.   jam_pulang TIME,
  20.   terlambat TIME,
  21.   ket VARCHAR(30) NOT NULL
  22. ) ENGINE=MyISAM;
  23.  
  24. INSERT INTO tb_presensi
  25. VALUES
  26.   ('123456789','2014-10-15','11:30:17','00:00:00','03:30:00','masuk'),
  27.   ('1234567890','2014-10-11','12:25:01','12:26:27','04:25:00','masuk'),
  28.   ('1234567890','2014-10-12','15:02:42','00:00:00','07:02:00','masuk'),
  29.   ('1234567890','2014-10-14','09:48:47','10:42:53','01:48:00','masuk');
  30. */
  31. $dbhost='localhost';
  32. $dbuser='root';
  33. $dbpass='';
  34. $dbname='test';
  35. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  36. $sql='SELECT nik,nama FROM tb_karyawan';
  37. $result=$db->query($sql);
  38. $karyawan=array();
  39. while($data=$result->fetch_assoc()){
  40.   $karyawan[]=$data;
  41. }
  42. $sql='SELECT nik,tanggal,jam_masuk,jam_pulang,terlambat,ket FROM tb_presensi ORDER BY tanggal';
  43. $result=$db->query($sql);
  44. $presensi=array();
  45. $tanggal=array();
  46. while($data=$result->fetch_assoc()){
  47.   if(isset($presensi[$data['tanggal']])){
  48.     $presensi[$data['tanggal']][$data['nik']]=$data;
  49.   }else{
  50.     $presensi[$data['tanggal']]=array();
  51.     $presensi[$data['tanggal']][$data['nik']]=$data;
  52.   }
  53.   $tanggal[]=$data['tanggal'];
  54. }
  55. $min_date=min($tanggal);
  56. $max_date=max($tanggal);
  57. echo "<table border='1'>";
  58. echo "<tr><th>No</th>";
  59. echo "<th>Tanggal</th>";
  60. echo "<th>NIK</th>";
  61. echo "<th>Nama</th>";
  62. echo "<th>Jam Masuk</th>";
  63. echo "<th>Jam Pulang</th>";
  64. echo "<th>Terlambat</th>";
  65. echo "<th>Keterangan</th></tr>";
  66. $i=0;
  67. while($min_date<=$max_date){
  68.   foreach($karyawan as $k){
  69.     echo "<tr><td>".(++$i)."</td>";
  70.     echo "<td>{$min_date}</td>";
  71.     echo "<td>{$k['nik']}</td>";
  72.     echo "<td>{$k['nama']}</td>";
  73.     if(isset($presensi[$min_date][$k['nik']])){
  74.       echo "<td>{$presensi[$min_date][$k['nik']]['jam_masuk']}</td>";
  75.       echo "<td>{$presensi[$min_date][$k['nik']]['jam_pulang']}</td>";
  76.       echo "<td>{$presensi[$min_date][$k['nik']]['terlambat']}</td>";
  77.       echo "<td>{$presensi[$min_date][$k['nik']]['ket']}</td>";
  78.     }else{
  79.       echo "<td>-</td>";
  80.       echo "<td>-</td>";
  81.       echo "<td>-</td>";
  82.       echo "<td>tidak masuk</td>";      
  83.     }
  84.     echo "</tr>";
  85.   }
  86.   $min_date=date('Y-m-d',strtotime($min_date.'+1 day'));
  87. }
  88. echo "</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement