Advertisement
cdsatrian

rangking multisort

Dec 19th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2. // mensimulasi data dari database, soalnya nggak tau tabel dan datanya sih...
  3. $kritA=array(
  4.         array("id_kriteria"=>1,"kriteria"=>"Pendidikan","nilai"=>100),
  5.         array("id_kriteria"=>2,"kriteria"=>"Kelengkapan Dokumen","nilai"=>100),
  6.         array("id_kriteria"=>3,"kriteria"=>"Pengalaman Kerja","nilai"=>100)
  7.       );
  8. $no_urutA=array(
  9.           array("no_urut"=>1245,"nilai_1"=>2.43,"nilai_2"=>1.485,"nilai_3"=>1.07),
  10.           array("no_urut"=>1246,"nilai_1"=>2.595,"nilai_2"=>1.8,"nilai_3"=>0.94),
  11.           array("no_urut"=>1247,"nilai_1"=>2.185,"nilai_2"=>1.39,"nilai_3"=>1.25),
  12.          );
  13. function array_to_object($array) {
  14.   $obj = new stdClass;
  15.   foreach($array as $k => $v) {
  16.     $obj->{$k} =(is_array($v))?array_to_object($v): $v;
  17.   }
  18.   return $obj;
  19. }
  20. // ini pura-puranya hasil dari query ke database-nya...
  21. $krit=array_to_object($kritA);
  22. $no_urut=array_to_object($no_urutA);
  23. // preparing data to show
  24. $no = 0;
  25. $data_fix=array();
  26. foreach($no_urut as $row1){
  27.   $data_fix[$no]['no_urut']=$row1->no_urut;
  28.   $jum = 0;
  29.   foreach($krit as $row){
  30.     $id = $row->id_kriteria;    
  31.     $nn = $row1->{"nilai_$id"};
  32.     $nnn = $row->nilai/100*$nn;
  33.     $data_fix[$no][$row->kriteria] = $nnn;
  34.     $jum+=$nnn;
  35.   }
  36.   $data_fix[$no]['hasil_akhir'] = $jum;    
  37.   $hasil_akhir=array();
  38.   foreach($data_fix as $key => $row6){
  39.     $hasil_akhir[$key]=$row6['hasil_akhir'];
  40.   }
  41.   $no++;
  42. }
  43. array_multisort($hasil_akhir,SORT_DESC,$data_fix);
  44. ?>
  45. <html>
  46.  <head>
  47.   <title>Array Multisort</title>
  48.  </head>
  49. <body>
  50. <table border="1">
  51.   <thead>
  52.     <tr>
  53.       <th>No. Urut Pelamar</th>
  54.       <?php
  55.         foreach($krit as $row){
  56.           echo "<th scope='col'>$row->kriteria</th>";
  57.         }
  58.       ?>
  59.       <th>Hasil Akhir</th>
  60.       <th>Rangking</th>
  61.     </tr>
  62.   </thead>
  63.   <tbody>
  64.   <?php
  65.   $rangking=1;
  66.   foreach($data_fix as $key=>$val){
  67.     echo "<tr>";
  68.     foreach($val as $k=>$v){
  69.       echo "<td>".$v."</td>";
  70.     }
  71.     echo "<td>$rangking</td></tr>\n";
  72.     $rangking++;
  73.   }
  74.   ?>
  75.   </tbody>
  76. </table>
  77. </body>
  78. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement