Read 379 times | Created 2014-05-14 06:35:11 | Updated 2014-05-14 06:35:11 | | |

 

==========================================
*** FILE : tahani.php
==========================================

<?php
/*********************************
FILENAME  : tahani.php
CREATE BY  : cahya dsn
PURPOSE   : Tahani Fuzzy Database Engine
CREATE DATE : 2014-05-14
**********************************
#table creation

USE `test`;

DROP TABLE IF EXISTS `tbl_karyawan`;
CREATE TABLE IF NOT EXISTS `tbl_karyawan` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `nama` varchar(30) NOT NULL,
 `usia` tinyint(3) unsigned NOT NULL,
 `masa_kerja` tinyint(3) unsigned NOT NULL,
 `gaji` float NOT NULL,
 PRIMARY KEY(`id`)
) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data karyawan';

INSERT INTO `tbl_karyawan`(`nama`,`usia`,`gaji`,`masa_kerja`)
VALUES
('Susi','30','750000','6'), 
('Adi','48','1255000','17'),
('Tia','36','1500000','14'),
('Lusi','37','1040000','4 '),
('Siska','42','950000','12'),
('Andy','39','1600000','13'),
('Tutik','37','1250000','5'), 
('Yoga','32','550000','1'), 
('Rina','35','735000','3'), 
('Kiki','25','860000','2');

DROP TABLE IF EXISTS `tbl_fuzzy`;
CREATE TABLE IF NOT EXISTS `tbl_fuzzy` (
 `id` int(10) unsigned NOT NULL,
 `usia_muda` float NOT NULL,
 `usia_parobaya` float NOT NULL,
 `usia_tua` float NOT NULL,  
 `masa_kerja_baru` float NOT NULL,
 `masa_kerja_lama` float NOT NULL,
 `gaji_rendah` float NOT NULL,
 `gaji_sedang` float NOT NULL,
 `gaji_tinggi` float NOT NULL
) ENGINE=MyISAM;
*/
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$x_muda_min=isset($_POST['x_muda_min'])?$_POST['x_muda_min']:30;
$x_muda_max=isset($_POST['x_muda_max'])?$_POST['x_muda_max']:40;
$x_parobaya_min=isset($_POST['x_parobaya_min'])?$_POST['x_parobaya_min']:35;
$x_parobaya_mid=isset($_POST['x_parobaya_mid'])?$_POST['x_parobaya_mid']:45;
$x_parobaya_max=isset($_POST['x_parobaya_max'])?$_POST['x_parobaya_max']:50;
$x_tua_min=isset($_POST['x_tua_min'])?$_POST['x_tua_min']:40;
$x_tua_max=isset($_POST['x_tua_max'])?$_POST['x_tua_max']:50;
$y_baru_min=isset($_POST['y_baru_min'])?$_POST['y_baru_min']:5;
$y_baru_max=isset($_POST['y_baru_max'])?$_POST['y_baru_max']:15;
$y_lama_min=isset($_POST['y_lama_min'])?$_POST['y_lama_min']:10;
$y_lama_max=isset($_POST['y_lama_max'])?$_POST['y_lama_max']:25;
$z_rendah_min=isset($_POST['z_rendah_min'])?$_POST['z_rendah_min']:300000;
$z_rendah_max=isset($_POST['z_rendah_max'])?$_POST['z_rendah_max']:800000;
$z_sedang_min=isset($_POST['z_sedang_min'])?$_POST['z_sedang_min']:500000;
$z_sedang_mid=isset($_POST['z_sedang_mid'])?$_POST['z_sedang_mid']:1000000;
$z_sedang_max=isset($_POST['z_sedang_max'])?$_POST['z_sedang_max']:1500000;
$z_tinggi_min=isset($_POST['z_tinggi_min'])?$_POST['z_tinggi_min']:1000000;
$z_tinggi_max=isset($_POST['z_tinggi_max'])?$_POST['z_tinggi_max']:2000000;
?>
<!DOCTYPE html>
<html>
  <head>
    <title>protoTahani</title>
    <style type='text/css'>
      * {font-family:verdana,arial,sans-serif;font-size:10pt;}
      h1{font-size:18pt;}
      h2{font-size:14pt;line-height:16pt;}
      fieldset{margin:5px;padding:5px;background-color:#eee;}
      legend {font-weight:bold;padding:5px;background-color:#ee9;}
      .inptxt{text-align:right;width:100px;}
    </style>
  </head>
  <body>
    <div id='container'>
      <h2>Fuzzy Database</h2>
      <h1>Metode Tahani</h1>
      <fieldset style='display:none'>
        <legend>Kasus</legend>
        <!-- reserved //-->
      </fieldset>
      <form method='post'>
        <fieldset>
          <legend>Data Karyawan</legend>
          <table border='1'>
            <tr><th>No</th><th>Nama</th><th>Usia</th><th>Masa Kerja</th><th>Gaji</th></tr>
            <?php
            $sql="SELECT * FROM tbl_karyawan";
            $result=$db->query($sql);
            $i=0;
            while($record=$result->fetch_object()){
              echo "<tr>"
                  ."<td align='right'>".(++$i)."</td>"
                  ."<td>{$record->nama}</td>"
                  ."<td align='center'>{$record->usia}</td>"
                  ."<td align='center'>{$record->masa_kerja}</td>"
                  ."<td align='right'>".number_format($record->gaji)."</td>"
                  ."</tr>n";
            }
            ?>
          </table>
        </fieldset>
        <fieldset>
          <legend>Batasan</legend>
          <table border='1'>
            <tr><th>Variabel</th><th>Himpunan</th><th>Value</th></tr>
            <tr>
              <td rowspan='3'>Usia (x)</td>
              <td>Muda</td>
              <td>
                min :<input type='text' class='inptxt' name='x_muda_min' value='<?=$x_muda_min?>'/>
                max :<input type='text' class='inptxt' name='x_muda_max' value='<?=$x_muda_max?>'/>
              </td>
            </tr>
            <tr>
              <td>Parobaya</td>
              <td>
                min :<input type='text' class='inptxt' name='x_parobaya_min' value='<?=$x_parobaya_min?>'/>
                mid :<input type='text' class='inptxt' name='x_parobaya_mid' value='<?=$x_parobaya_mid?>'/>
                max :<input type='text' class='inptxt' name='x_parobaya_max' value='<?=$x_parobaya_max?>'/>
              </td>
            </tr>
            <tr>
              <td>Tua</td>
              <td>
                min :<input type='text' class='inptxt' name='x_tua_min' value='<?=$x_tua_min?>'/>
                max :<input type='text' class='inptxt' name='x_tua_max' value='<?=$x_tua_max?>'/>
              </td>
            </tr>
            <tr>
              <td rowspan='2'>Masa Kerja (y)</td>
              <td>Baru</td>
              <td>
                min :<input type='text' class='inptxt' name='y_baru_min' value='<?=$y_baru_min?>'/>
                max :<input type='text' class='inptxt' name='y_baru_max' value='<?=$y_baru_max?>'/>
              </td>
            </tr>
            <tr>
              <td>Lama</td>
              <td>
                min :<input type='text' class='inptxt' name='y_lama_min' value='<?=$y_lama_min?>'/>
                max :<input type='text' class='inptxt' name='y_lama_max' value='<?=$y_lama_max?>'/>
              </td>
            </tr>
            <tr>
              <td rowspan='3'>Gaji (z)</td>
              <td>Rendah</td>
              <td>
                min :<input type='text' class='inptxt' name='z_rendah_min' value='<?=$z_rendah_min?>'/>
                max :<input type='text' class='inptxt' name='z_rendah_max' value='<?=$z_rendah_max?>'/>
              </td>
            </tr>
            <tr>
              <td>Sedang</td>
              <td>
                min :<input type='text' class='inptxt' name='z_sedang_min' value='<?=$z_sedang_min?>'/>
                mid :<input type='text' class='inptxt' name='z_sedang_mid' value='<?=$z_sedang_mid?>'/>
                max :<input type='text' class='inptxt' name='z_sedang_max' value='<?=$z_sedang_max?>'/>
              </td>
            </tr>
            <tr>
              <td>Tinggi</td>
              <td>
                min :<input type='text' class='inptxt' name='z_tinggi_min' value='<?=$z_tinggi_min?>'/>
                max :<input type='text' class='inptxt' name='z_tinggi_max' value='<?=$z_tinggi_max?>'/>
              </td>
            </tr>
          </table>
        </fieldset>
        <input type='submit' name='proses' value='proses' />
      </form>
<?php
if(isset($_POST['proses'])){
?>        
      <fieldset>
        <legend>[1] Pembentukan Himpunan Fuzzy (Fuzzyfication)</legend>
        <table border='1'>
          <tr>
            <th colspan='2'>Himpunan Fuzzy untuk variabel Usia (x)</th>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>usia MUDA</sub>(x)</td>
            <td>1 , x<<?=$x_muda_min?></td>
          </tr>
          <tr>
            <td>(<?=$x_muda_max?>-x)/<?=($x_muda_max-$x_muda_min)?> , <?=$x_muda_min?> ≤ x ≤<?=$x_muda_max?></td>
          </tr>
          <tr>  
            <td>0 , x><?=$x_muda_max?></td>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>Usia PAROBAYA</sub>(x)</td>
            <td>0 , x<<?=$x_parobaya_min?> atau x><?=$x_parobaya_max?></td>
          </tr>
          <tr>
            <td>(x-<?=$x_parobaya_min?>)/<?=($x_parobaya_mid-$x_parobaya_min)?> , <?=$x_parobaya_min?> ≤ x ≤<?=$x_parobaya_mid?></td>
          </tr>
          <tr>
            <td>(<?=$x_parobaya_max?>-x)/<?=($x_parobaya_max-$x_parobaya_mid)?> , <?=$x_parobaya_mid?> ≤ x ≤<?=$x_parobaya_max?></td>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>usia TUA</sub>(x)</td>
            <td>0 , x<<?=$x_tua_min?></td>
          </tr>
          <tr>
            <td>(<?=$x_tua_max?>-x)/<?=($x_tua_max-$x_tua_min)?> , <?=$x_tua_min?> ≤ x ≤<?=$x_tua_max?></td>
          </tr>
          <tr>  
            <td>1 , x><?=$x_tua_max?></td>
          </tr>
          <tr>
            <th colspan='2'>Himpunan Fuzzy untuk variabel Masa kerja (y)</th>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>Masa kerja BARU</sub>(y)</td>
            <td>1 , y≤<?=$y_baru_min?></td>
          </tr>
          <tr>
            <td>(<?=$y_baru_max?>-y)/<?=($y_baru_max-$y_baru_min)?> , <?=$y_baru_min?> ≤ y ≤<?=$y_baru_max?></td>
          </tr>
          <tr>  
            <td>0 , y><?=$y_baru_max?></td>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>Masa kerja LAMA</sub>(y)</td>
            <td>0 , y≤<?=$y_lama_min?></td>
          </tr>
          <tr>
            <td>(y-<?=$y_lama_min?>)/<?=($y_lama_max-$y_lama_min)?> , <?=$y_lama_min?> ≤ y ≤<?=$y_lama_max?></td>
          </tr>
          <tr>  
            <td>1 , y><?=$y_lama_max?></td>
          </tr>
          <tr>
            <th colspan='2'>Himpunan Fuzzy untuk variabel Gaji (z)</th>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>gaji RENDAH</sub>(x)</td>
            <td>1 , z<<?=$z_rendah_min?></td>
          </tr>
          <tr>
            <td>(<?=$z_rendah_max?>-z)/<?=($z_rendah_max-$z_rendah_min)?> , <?=$z_rendah_min?> ≤ z ≤<?=$z_rendah_max?></td>
          </tr>
          <tr>  
            <td>0 , z><?=$z_rendah_max?></td>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>gaji SEDANG</sub>(x)</td>
            <td>0 , z<<?=$z_sedang_min?> atau z><?=$z_sedang_max?></td>
          </tr>
          <tr>
            <td>(z-<?=$z_sedang_min?>)/<?=($z_sedang_mid-$z_sedang_min)?> , <?=$z_sedang_min?> ≤ z ≤<?=$z_sedang_mid?></td>
          </tr>
          <tr>
            <td>(<?=$z_sedang_max?>-z)/<?=($z_sedang_max-$z_sedang_mid)?> , <?=$z_sedang_mid?> ≤ z ≤<?=$z_sedang_max?></td>
          </tr>
          <tr>
            <td rowspan='3'>µ<sub>gaji TINGGI</sub>(x)</td>
            <td>0 , z<<?=$z_tinggi_min?></td>
          </tr>
          <tr>
            <td>(z-<?=$z_tinggi_min?>)/<?=($z_tinggi_max-$z_tinggi_min)?> , <?=$z_tinggi_min?> ≤ z ≤<?=$z_tinggi_max?></td>
          </tr>
          <tr>  
            <td>1 , z><?=$z_tinggi_max?></td>
          </tr>
        </table>
      </fieldset>
      <fieldset>
        <legend>[2] Hasil Fuzzy Database Tahani</legend>
        <button id='btn_sql'>show sql</button>
        <button id='btn_sql_hide' style='display:none'>hide sql</button>
        <div id='sql' style='display:none'>
<?php
  $sql="
  TRUNCATE TABLE tbl_fuzzy;
  INSERT INTO tbl_fuzzy
  SELECT 
  id,
  CASE
    WHEN usia < $x_muda_min THEN 1
    WHEN usia >= $x_muda_min AND usia <= $x_muda_max THEN ($x_muda_max - usia)/($x_muda_max-$x_muda_min)
    ELSE 0
  END,
  CASE
    WHEN usia < $x_parobaya_min AND usia > $x_parobaya_max  THEN 0
    WHEN usia >= $x_parobaya_min AND usia <= $x_parobaya_mid THEN (usia-$x_parobaya_min)/($x_parobaya_mid-$x_parobaya_min)
    when usia > $x_parobaya_mid AND usia <= $x_parobaya_max THEN ($x_parobaya_max-usia)/($x_parobaya_max-$x_parobaya_mid)
  END,
  CASE
    WHEN usia < $x_tua_min THEN 0
    WHEN usia >= $x_tua_min AND usia <= $x_tua_max THEN ($x_tua_max - usia)/($x_tua_max-$x_tua_min)
    ELSE 1
  END,
  CASE
    WHEN masa_kerja < $y_baru_min THEN 1
    WHEN masa_kerja >= $y_baru_min AND masa_kerja <= $y_baru_max THEN ($y_baru_max - masa_kerja)/($y_baru_max-$y_baru_min)
    ELSE 0
  END,
  CASE
    WHEN masa_kerja < $y_lama_min THEN 0
    WHEN masa_kerja >= $y_lama_min AND masa_kerja <= $y_lama_max THEN (masa_kerja-$y_lama_min)/($y_lama_max-$y_lama_min)
    ELSE 1
  END,
  CASE
    WHEN gaji < $z_rendah_min THEN 1
    WHEN gaji >= $z_rendah_min AND gaji <= $z_rendah_max THEN ($z_rendah_max - gaji)/($z_rendah_max-$z_rendah_min)
    ELSE 0
  END,
  CASE
    WHEN gaji < $z_sedang_min AND gaji > $z_sedang_max  THEN 0
    WHEN gaji >= $z_sedang_min AND gaji <= $z_sedang_mid THEN (gaji-$z_sedang_min)/($z_sedang_mid-$z_sedang_min)
    WHEN gaji > $z_sedang_mid AND gaji <= $z_sedang_max THEN ($z_sedang_max-gaji)/($z_sedang_max-$z_sedang_mid)
  END,
  CASE
    WHEN gaji > $z_tinggi_max THEN 1
    WHEN gaji >= $z_tinggi_min AND gaji <= $z_tinggi_max THEN (gaji -$z_tinggi_min)/($z_tinggi_max-$z_tinggi_min)
    ELSE 0
  END
  FROM tbl_karyawan
  ";
  echo "<pre>$sql</pre></div>n";
  $db->multi_query($sql);
  while ($db->next_result()) {if(!$db->more_results()) break;} 
  $sql="SELECT 
          b.nama,
          a.usia_muda,
          a.usia_parobaya,
          a.usia_tua,
          a.masa_kerja_baru,
          a.masa_kerja_lama,
          a.gaji_rendah,
          a.gaji_sedang,
          a.gaji_tinggi
        FROM tbl_fuzzy a 
        JOIN tbl_karyawan b USING(id)";
  $result=$db->query($sql);
  echo "<table border='1'>n"
      ."<tr>"
      ."<th rowspan='2'>Nama</th>"
      ."<th colspan='3'>Usia</th>"
      ."<th colspan='2'>Masa Kerja</th>"
      ."<th colspan='3'>Gaji</th>"
      ."</tr>n"
      ."<tr>"
      ."<th>Muda</th>"
      ."<th>Parobaya</th>"
      ."<th>Tua</th>"
      ."<th>Baru</th>"
      ."<th>Lama</th>"
      ."<th>Rendah</th>"
      ."<th>Sedang</th>"
      ."<th>Tinggi</th>"
      ."</tr>";
  
  while($record=$result->fetch_row()){
    echo "<tr align='right'>";
    $i=0;
    foreach($record as $field){
      echo "<td".(++$i==1?" align='left'":"").">{$field}</td>";
    }
    echo "</tr>n";
  }
  echo "</table>n";
?>
      </fieldset>
      <fieldset>
        <legend>[3] Query</legend>
        <p>
            Cari Karyawan yang berusia
            <select id='usia'>
              <option value='1'>berapa saja</option>
              <option value='muda'>muda</option>
              <option value='parobaya'>parobaya</option>
              <option value='tua'>tua</option>
            </select>
            <select id='opr1'>
              <option value='AND'>dan</option>
              <option value='OR'>atau</option>
            </select>
            mempunyai masa kerja
            <select id='masa_kerja'>
              <option value='1'>berapa aja</option>
              <option value='baru'>masih baru</option>
              <option value='lama'>sudah lama</option>
            </select>
            <select id='opr2'>
              <option value='AND'>dan</option>
              <option value='OR'>atau</option>
            </select>
            memperoleh gaji
            <select id='gaji'>
              <option value='1'>berapa saja</option>
              <option value='rendah'>rendah</option>
              <option value='sedang'>sedang</option>
              <option value='tinggi'>tinggi</option>
            </select><br/>
            <button id='btn_cari'>cari</button>
          </p>
          <div id='hasil_query'></div>
      </fieldset>
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        $('#btn_sql').click(function(){
          $('#sql').toggle();
          $(this).hide();
          $('#btn_sql_hide').show();
        });
        $('#btn_sql_hide').click(function(){
          $('#sql').toggle();
          $(this).hide();
          $('#btn_sql').show();
        });
        $('#btn_cari').click(function(){
          $.post('tahani_query.php',
                 {usia:$('#usia').val(),opr1:$('#opr1').val(),masa_kerja:$('#masa_kerja').val(),opr2:$('#opr2').val(),gaji:$('#gaji').val()},
                 function( data ) {
                    $('#hasil_query').html(data);
                 });
        });
    </script>
<?php  
}
?>
  



</body>
</html>

=======================
**** FILE : tahani_query.php
=======================
<?php
/*********************************
FILENAME  : tahani_query.php
CREATE BY  : cahya dsn
PURPOSE   : queris data to table fuzzy
CREATE DATE : 2014-05-14
**********************************/
if(isset($_POST)){
  $dbhost='localhost';
  $dbuser='root';
  $dbpass='';
  $dbname='test';
  $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  $sql="SELECT GROUP_CONCAT(a.nama) AS karyawan
        FROM tbl_karyawan a
        JOIN tbl_fuzzy b USING(id)
        WHERE "
        .($_POST['usia']==1?' 1 ':' b.usia_'.$_POST['usia'].'>0 ')
        .$_POST['opr1']
        .($_POST['masa_kerja']==1?' 1 ':' b.masa_kerja_'.$_POST['masa_kerja'].'>0 ')
        .$_POST['opr2']
        .($_POST['gaji']==1?' 1 ':' b.gaji_'.$_POST['gaji'].'>0 ');
  $result=$db->query($sql);
  $data=$result->fetch_object();
  echo "<b>Karyawan :</b>".$data->karyawan;
};