Advertisement
cdsatrian

absence monthly report

Dec 18th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. /*********************************
  3. FILENAME    : absen.php
  4. CREATE BY   : cahya dsn
  5. PURPOSE     : display monthly absence report
  6. CREATE DATE : 2012-12-18
  7. **********************************
  8. #table creation
  9.  
  10. USE `test`;
  11.  
  12. DROP TABLE IF EXISTS `tbl_siswa`;
  13. CREATE TABLE IF NOT EXISTS `tbl_siswa` (
  14.   `id_siswa` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table tbl_siswa',
  15.   `nama` varchar(30) NOT NULL COMMENT 'nama siswa',
  16.   PRIMARY KEY(`id_siswa`)
  17. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data siswa';
  18.  
  19. INSERT INTO `tbl_siswa`(`id_siswa`,`nama`) VALUES
  20. (NULL,'Agus'),(NULL,'Budi'),(NULL,'Citra'),(NULL,'Dian'),(NULL,'Edo');
  21.  
  22. DROP TABLE IF EXISTS `tbl_absen`;
  23. CREATE TABLE IF NOT EXISTS `tbl_absen` (
  24.   `id_absen` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table tbl_absen',
  25.   `id_siswa` bigint(20) unsigned NOT NULL COMMENT 'foreign key ke tabel tbl_siswa',
  26.   `absen_date` date NOT NULL COMMENT 'tanggal absen',
  27.   `status` ENUM('a','i','s') NOT NULL COMMENT 'status absen',
  28.   PRIMARY KEY (`id_absen`)
  29. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data absensi';
  30.  
  31. INSERT INTO `tbl_absen`(`id_absen`,`id_siswa`,`absen_date`,`status`) VALUES
  32. (NULL,1,'2012-12-01','a'),(NULL,3,'2012-12-01','s'),(NULL,3,'2012-12-02','s'),
  33. (NULL,4,'2012-12-04','i'),(NULL,2,'2012-12-07','a'),(NULL,1,'2012-12-15','s'),
  34. (NULL,2,'2012-12-20','s'),(NULL,2,'2012-12-21','s');
  35. */
  36. //database configuration
  37. $dbhost='localhost';
  38. $dbuser='root';
  39. $dbpass='';
  40. $dbname='test';
  41. //database connection
  42. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  43. //query to get absen datas for current month
  44. $sql="SELECT b.nama,a.absen_date,a.status FROM tbl_absen a INNER JOIN tbl_siswa b ON a.id_siswa=b.id_siswa "
  45.     ."WHERE a.absen_date BETWEEN '".date('Y-m-1')."' AND '".date('Y-m-t')."' "
  46.     ."ORDER BY b.nama, a.absen_date ";
  47. $result=$db->query($sql);
  48. //variables initialization
  49. $absen=array();
  50. $nama='';
  51. //generate absen datas from database
  52. while($records=$result->fetch_object()){
  53.   if($records->nama!=$nama){
  54.     $nama=$records->nama;
  55.     $absen[$nama]=array();
  56.   }
  57.   $absen[$nama][substr($records->absen_date,-2)]=$records->status;
  58. }
  59. //get number of days in given month (current month)
  60. $num_days=date("t");
  61. ?>
  62. <!DOCTYPE html>
  63. <html>
  64. <head>
  65. <title>pelajaran table session 1</title>
  66. <style>
  67. table {padding:1px;}
  68. .a {background-color:#c00;}
  69. .s {background-color:#0c0;}
  70. .i {background-color:#00c;}
  71. td {text-align:center;padding:1px 5px;}
  72. </style>
  73. </head>
  74. <body>
  75. <table border=1>
  76. <tr><th colspan="<?php echo (1+$num_days);?>"><?php echo date("F Y");?></th></tr>
  77. <tr><th rowspan="2">Nama</th><th colspan="<?php echo ($num_days);?>">Tanggal</th></tr>
  78. <tr><?php for($i=1;$i<=$num_days;$i++){ echo "<th>$i</th>";}?></tr>
  79. <?php
  80. foreach($absen as $key=>$value){
  81.   echo "<tr><td>".$key."</td>";
  82.   for($i=1;$i<=$num_days;$i++){
  83.     echo "<td"
  84.         .(
  85.           isset($value[$i<10?'0'.$i:$i])
  86.           ?
  87.           ' class="'.$value[$i<10?'0'.$i:$i].'">'.$value[$i<10?'0'.$i:$i]
  88.           :
  89.           '>&nbsp;'
  90.          )
  91.         ."</td>";
  92.   }
  93.   echo "</tr>\n";
  94. }
  95. ?>
  96. </table>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement