Advertisement
cdsatrian

Data on Matrix form

Sep 22nd, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2. /*
  3.  USE test;
  4.  
  5. DROP TABLE IF EXISTS `pegawai`;  
  6. CREATE TABLE IF NOT EXISTS `pegawai` (  
  7.  `nik_pegawai` CHAR(10) NOT NULL,  
  8.  `nama` varchar(25) NOT NULL,  
  9.  PRIMARY KEY(`nik_pegawai`)    
  10. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data pegawai';  
  11. INSERT INTO `pegawai`(`nik_pegawai`,`nama`)
  12. VALUES
  13.  ('2007090301','Agus Pambagyo'),
  14.  ('2007091002','Budi Setiawan'),
  15.  ('2007091703','Cindy Fitriani'),
  16.  ('2007092304','Desi Anggraeni'),
  17.  ('2007092805','Eman Sutarman'),
  18.  ('2007100706','Firmansyah'),
  19.  ('2007101207','Gina Fauzia');
  20. DROP TABLE IF EXISTS `nilai_kriteria`;  
  21. CREATE TABLE IF NOT EXISTS `nilai_kriteria` (  
  22.  `nik_pegawai` CHAR(10) NOT NULL,  
  23.  `nilai` FLOAT NOT NULL DEFAULT 0,
  24.  `kode_kriteria` CHAR(3) NOT NULL  
  25. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data nilai kriteria';  
  26. INSERT INTO `nilai_kriteria`(`nik_pegawai`,`nilai`,`kode_kriteria`)
  27. VALUES
  28. ('2007090301',8.23,'K01'),
  29. ('2007090301',8.67,'K02'),
  30. ('2007090301',9.43,'K03'),  
  31. ('2007090301',5.43,'K04'),
  32. ('2007090301',6.25,'K05'),  
  33. ('2007090301',8.21,'K06'),
  34. ('2007091002',8.47,'K01'),
  35. ('2007091002',8.55,'K02'),
  36. ('2007091002',7.83,'K03'),  
  37. ('2007091002',6.28,'K04'),
  38. ('2007091002',7.93,'K05'),  
  39. ('2007091002',7.43,'K06'),
  40. ('2007091703',8.26,'K01'),
  41. ('2007091703',8.81,'K02'),
  42. ('2007091703',7.22,'K03'),  
  43. ('2007091703',5.93,'K04'),
  44. ('2007091703',8.75,'K05'),  
  45. ('2007091703',7.25,'K06'),
  46. ('2007092304',7.83,'K01'),
  47. ('2007092304',7.43,'K02'),
  48. ('2007092304',8.90,'K03'),  
  49. ('2007092304',9.21,'K04'),
  50. ('2007092304',7.83,'K05'),  
  51. ('2007092304',8.77,'K06'),
  52. ('2007092805',8.23,'K01'),
  53. ('2007092805',7.33,'K02'),
  54. ('2007092805',8.32,'K03'),  
  55. ('2007092805',9.21,'K04'),
  56. ('2007092805',5.83,'K05'),  
  57. ('2007092805',8.53,'K06'),
  58. ('2007100706',7.39,'K01'),
  59. ('2007100706',8.32,'K02'),
  60. ('2007100706',8.91,'K03'),  
  61. ('2007100706',9.51,'K04'),
  62. ('2007100706',8.96,'K05'),  
  63. ('2007100706',7.27,'K06'),
  64. ('2007101207',7.28,'K01'),
  65. ('2007101207',6.56,'K02'),
  66. ('2007101207',8.20,'K03'),  
  67. ('2007101207',9.13,'K04'),
  68. ('2007101207',7.25,'K05'),  
  69. ('2007101207',6.43,'K06');
  70.  */
  71. ?>
  72. <!DOCTYPE html>
  73. <html>
  74.     <head>
  75.         <title>Bobot</title>
  76.         <style>
  77.             * {font-family: verdana,arial,sans-serif;font-size:12px;color:#339;}
  78.             td{text-align: center;}
  79.         </style>
  80.     </head>
  81.     <body>
  82.         <div id='container'>
  83. <?php
  84. $dbhost='localhost';
  85. $dbuser='root';
  86. $dbpass='';
  87. $dbname='test';
  88. $db=new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  89. $sql="
  90. SELECT
  91.    a.nik_pegawai,
  92.    GROUP_CONCAT(a.kode_kriteria) AS kriteria,
  93.    GROUP_CONCAT(a.nilai/c.maks) AS bobot
  94. FROM nilai_kriteria a
  95.   JOIN pegawai b USING(nik_pegawai)
  96.   JOIN (
  97.     SELECT kode_kriteria,MAX(nilai) AS maks
  98.     FROM nilai_kriteria
  99.     GROUP BY kode_kriteria
  100.   ) c USING(kode_kriteria)
  101. GROUP BY b.nik_pegawai
  102. ";
  103. $data=array();
  104. $kode=array();
  105. if($result=$db->query($sql))
  106. {
  107.   while($record = $result->fetch_object())
  108.   {
  109.     $kriteria=explode(",",$record->kriteria);
  110.     $kode=array_merge($kode,$kriteria);
  111.     $bobot=explode(",",$record->bobot);
  112.     $num_col=count($kriteria);
  113.     if(isset($data[$record->nik_pegawai]))
  114.     {
  115.       for($i=0;$i<$num_col;$i++)
  116.       {
  117.         $data[$record->nik_pegawai][$kriteria[$i]]=$bobot[$i];
  118.       }
  119.     }
  120.     else
  121.     {
  122.       $data[$record->nik_pegawai]=array();
  123.       for($i=0;$i<$num_col;$i++)
  124.       {
  125.         $data[$record->nik_pegawai]['nik']=$record->nik_pegawai;  
  126.         $data[$record->nik_pegawai][$kriteria[$i]]=$bobot[$i];
  127.       }
  128.     }
  129.   }
  130.   $kode_kriteria=array_unique($kode);
  131.   $num=count($kode_kriteria);
  132.   echo "<table border='1'>\n"
  133.       ."<tr><th>NIK\Kriteria</th>";
  134.   foreach($kode_kriteria as $kd)
  135.   {    
  136.       echo "<th>".$kd."</th>";
  137.   }
  138.   echo "</tr>\n";
  139.   foreach($data as $dt)
  140.   {
  141.     echo "<tr>"
  142.          ."<th>".$dt['nik']."</th>";
  143.     for($j=0;$j<$num;$j++)
  144.     {
  145.       echo "<td>".(isset($dt[$kode_kriteria[$j]])?$dt[$kode_kriteria[$j]]:"&nbsp;")."</td>";
  146.     }
  147.     echo "</tr>\n";
  148.   }
  149.   echo "</table>";
  150. }
  151. ?>
  152.         </div>
  153.     </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement