Advertisement
cdsatrian

simulasi rangking PSB

Oct 21st, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.44 KB | None | 0 0
  1. <?php
  2. /************************************
  3. filename     : pmb.php
  4. created by   : Cahya DSN
  5. created date : 2012-10-22
  6. purpose      : simulasi pemeringkatan
  7.                penerimaan siswa baru
  8. ************************************/
  9. /*
  10. -- SAMPLE DATABASE
  11.  
  12. -- use `test`
  13.  
  14. DROP TABLE IF EXISTS `sekolah`;
  15. CREATE TABLE IF NOT EXISTS `sekolah` (
  16.   `sekolah_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table sekolah',
  17.   `sekolah_nama` varchar(255) NOT NULL COMMENT 'nama sekolah',
  18.   `sekolah_kuota` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'jumlah kuota penerimaan siswa',
  19.   PRIMARY KEY (`sekolah_id`)
  20. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Tabel untuk menyimpan data nama sekolah berserta kuotanya';
  21.  
  22.  
  23. INSERT INTO `test`.`sekolah` (`sekolah_id`, `sekolah_nama`, `sekolah_kuota`)
  24. VALUES
  25. (NULL, 'SMA Kepanjen', '3'),
  26. (NULL, 'SMA Sumberpucung', '4'),
  27. (NULL, 'SMA Lawang', '3'),
  28. (NULL, 'SMA Tumpang', '5');
  29.  
  30.  
  31. DROP TABLE IF EXISTS `pendaftar`;
  32. CREATE TABLE IF NOT EXISTS `pendaftar` (
  33.   `pendaftar_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table pendaftar',
  34.   `pendaftar_nama` varchar(255) NOT NULL COMMENT 'nama pendaftar',
  35.   `pendaftar_nilai` float(5,2) NOT NULL COMMENT 'nilai pendaftar',
  36.   `pendaftar_pil1` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'id sekolah pilihan 1',
  37.   `pendaftar_pil2` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'id sekolah pilihan 2',
  38.   PRIMARY KEY (`pendaftar_id`)
  39. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Tabel untuk menyimpan data nama pendaftar dan sekolah pilihannya';
  40.  
  41. INSERT INTO `test`.`pendaftar` (`pendaftar_id`, `pendaftar_nama`, `pendaftar_nilai`, `pendaftar_pil1`, `pendaftar_pil2`)
  42. VALUES
  43. (NULL,'A','21.45','2','3'),
  44. (NULL,'B','28.16','1','3'),
  45. (NULL,'C','28.64','1','4'),
  46. (NULL,'D','29.48','3','2'),
  47. (NULL,'E','26.75','3','4'),
  48. (NULL,'F','24.00','4','2'),
  49. (NULL,'G','28.74','2','3'),
  50. (NULL,'H','26.13','2','1'),
  51. (NULL,'I','23.56','4','3'),
  52. (NULL,'J','24.97','2','1'),
  53. (NULL,'K','28.59','2','4'),
  54. (NULL,'L','26.82','1','3'),
  55. (NULL,'M','22.14','2','1'),
  56. (NULL,'N','27.64','1','4'),
  57. (NULL,'O','25.99','3','2'),
  58. (NULL,'P','25.61','1','4'),
  59. (NULL,'Q','27.82','3','1'),
  60. (NULL,'R','23.57','2','3'),
  61. (NULL,'S','23.76','4','2'),
  62. (NULL,'T','21.57','2','3'),
  63. (NULL,'U','20.89','2','3'),
  64. (NULL,'V','29.34','1','4'),
  65. (NULL,'W','29.73','3','1'),
  66. (NULL,'X','28.77','4','1'),
  67. (NULL,'Y','26.69','3','4'),
  68. (NULL,'Z','26.48','2','2');
  69. */
  70. //-- DATABASE CONFIGURATIION
  71. $dbhost='localhost';
  72. $dbuser='test';
  73. $dbpass='test';
  74. $dbname='test';
  75. //--- Database connection
  76. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  77. //--- variable initialization
  78. $sekolah=array();
  79. $kuota=array();
  80. $index=array();
  81. $kuota[0]=array();
  82. $i=0;
  83. //--- ambil data sekolah dan kuotanya
  84. $sql="SELECT * FROM `sekolah`";
  85. $result=$db->query($sql);
  86. while($row=$result->fetch_object()){
  87.   $index[$i]=$row->sekolah_id;
  88.   $sekolah[$index[$i]]=array($row->sekolah_nama,$row->sekolah_kuota);
  89.   $kuota[$index[$i]]=array();
  90.   $i++;
  91. }
  92. $result->close();
  93. //--- ambil data pendaftar
  94. $sql="SELECT * FROM `pendaftar` ORDER BY `pendaftar_nilai` DESC";
  95. $result=$db->query($sql);
  96. //---- pengalokasian berdasarkan nilai dan kuota
  97. while($row=$result->fetch_object())
  98. {
  99.   if(count($kuota[$row->pendaftar_pil1])<$sekolah[$row->pendaftar_pil1][1])
  100.   {
  101.     $kuota[$row->pendaftar_pil1][]=array($row->pendaftar_id,$row->pendaftar_nama,$row->pendaftar_nilai);
  102.   }
  103.   elseif(count($kuota[$row->pendaftar_pil2])<$sekolah[$row->pendaftar_pil2][1])
  104.   {
  105.     $kuota[$row->pendaftar_pil2][]=array($row->pendaftar_id,$row->pendaftar_nama,$row->pendaftar_nilai);
  106.   }
  107.   else
  108.   {
  109.     $kuota[0][]=array($row->pendaftar_id,$row->pendaftar_nama,$row->pendaftar_nilai);
  110.   }
  111. }
  112. $result->close();
  113. $db->close();
  114. ?><!DOCTYPE html>
  115. <html>
  116. <head>
  117.   <title>Penerimaan Siswa Baru</title>
  118.   <style type="text/css">
  119.     html,body{padding:0px;margin:0px;font-family:verdana,arial,sans-serif;font-size:14px;color:#009;}
  120.     .container {margin:5px;padding 5px;width:250px;clear:both;}
  121.     td,th {border:solid 1px #999;margin:0px;padding:2px;}
  122.   </style>
  123. </head>
  124. <body>
  125.   <div class="container">
  126. <?php
  127. //----- TAMPILAN HASIL PERHITUNGAN
  128. //----- daftar yang diterima
  129. echo "<h4>Daftar Peringkat</h4>";
  130. for($i=0;$i<count($sekolah);$i++){
  131.   echo "<fieldset>"
  132.       ."<legend><strong>".$sekolah[$index[$i]][0]."</strong></legend>\n"
  133.       ."Jumlah Kuota : <strong>".$sekolah[$index[$i]][1]."</strong><br>\n";
  134.   $num=count($kuota[$index[$i]]);
  135.   echo "<table>\n"
  136.       ."<tr>"
  137.       ."<th>Rangking</th>"
  138.       ."<th>ID</th>"
  139.       ."<th>Nama</th>"
  140.       ."<th>Nilai</th>"
  141.       ."</tr>\n";
  142.   for($j=0;$j<$num;$j++){
  143.     echo "<tr><td>".($j+1)."</td>"
  144.         ."<td>".$kuota[$index[$i]][$j][0]."</td>"
  145.         ."<td>".$kuota[$index[$i]][$j][1]."</td>"
  146.         ."<td>".$kuota[$index[$i]][$j][2]."</td>"
  147.         ."</tr>\n";
  148.   }
  149.   echo "</table>\n"
  150.       ."</fieldset>\n";
  151. }
  152. //--- daftar yang nggak diterima
  153. echo "<h4>Unalocated</h4>";
  154. $num=count($kuota[0]);
  155. echo "<table>\n"
  156.     ."<tr>"
  157.     ."<th>Rangking</th>"
  158.     ."<th>ID</th>"
  159.     ."<th>Nama</th>"
  160.     ."<th>Nilai</th>"
  161.     ."</tr>\n";
  162. for($i=0;$i<$num;$i++){
  163.   echo "<tr>"
  164.       ."<td>".($i+1)."</td>"
  165.       ."<td>".$kuota[0][$i][0]."</td>"
  166.       ."<td>".$kuota[0][$i][1]."</td>"
  167.       ."<td>".$kuota[0][$i][2]."</td>"
  168.       ."</tr>\n";
  169. }
  170. echo "</table>\n</p>\n";
  171. ?>
  172. </div>
  173. </body>
  174. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement