eremaka

fungsi.php

Sep 28th, 2020
826
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php error_reporting (E_ALL ^ E_NOTICE); ?>
  2. <?php
  3.  
  4. // mencari ID kriteria
  5. // berdasarkan urutan ke berapa (C1, C2, C3)
  6. function getKriteriaID($no_urut) {
  7.     include('config.php');
  8.     $query  = "SELECT id FROM kriteria ORDER BY id";
  9.     $result = mysqli_query($koneksi, $query);
  10.  
  11.     while ($row = mysqli_fetch_array($result)) {
  12.         $listID[] = $row['id'];
  13.     }
  14.  
  15.     return $listID[($no_urut)];
  16. }
  17.  
  18. // mencari ID alternatif
  19. // berdasarkan urutan ke berapa (A1, A2, A3)
  20. function getAlternatifID($no_urut) {
  21.     include('config.php');
  22.     $query  = "SELECT id FROM alternatif ORDER BY id";
  23.     $result = mysqli_query($koneksi, $query);
  24.  
  25.     while ($row = mysqli_fetch_array($result)) {
  26.         $listID[] = $row['id'];
  27.     }
  28.  
  29.     return $listID[($no_urut)];
  30. }
  31.  
  32. // mencari nama kriteria
  33. function getKriteriaNama($no_urut) {
  34.     include('config.php');
  35.     $query  = "SELECT nama FROM kriteria ORDER BY id";
  36.     $result = mysqli_query($koneksi, $query);
  37.  
  38.     while ($row = mysqli_fetch_array($result)) {
  39.         $nama[] = $row['nama'];
  40.     }
  41.  
  42.     return $nama[($no_urut)];
  43. }
  44.  
  45. // mencari nama alternatif
  46. function getAlternatifNama($no_urut) {
  47.     include('config.php');
  48.     $query  = "SELECT nama FROM alternatif ORDER BY id";
  49.     $result = mysqli_query($koneksi, $query);
  50.  
  51.     while ($row = mysqli_fetch_array($result)) {
  52.         $nama[] = $row['nama'];
  53.     }
  54.  
  55.     return $nama[($no_urut)];
  56. }
  57.  
  58. // mencari priority vector alternatif
  59. function getAlternatifPV($id_alternatif,$id_kriteria) {
  60.     include('config.php');
  61.     $query = "SELECT nilai FROM pv_alternatif WHERE id_alternatif=$id_alternatif AND id_kriteria=$id_kriteria";
  62.     $result = mysqli_query($koneksi, $query);
  63.     while ($row = mysqli_fetch_array($result)) {
  64.         $pv = $row['nilai'];
  65.     }
  66.  
  67.     return $pv;
  68. }
  69.  
  70. // mencari priority vector kriteria
  71. function getKriteriaPV($id_kriteria) {
  72.     include('config.php');
  73.     $query = "SELECT nilai FROM pv_kriteria WHERE id_kriteria=$id_kriteria";
  74.     $result = mysqli_query($koneksi, $query);
  75.     while ($row = mysqli_fetch_array($result)) {
  76.         $pv = $row['nilai'];
  77.     }
  78.  
  79.     return $pv;
  80. }
  81.  
  82. // mencari jumlah alternatif
  83. function getJumlahAlternatif() {
  84.     include('config.php');
  85.     $query  = "SELECT count(*) FROM alternatif";
  86.     $result = mysqli_query($koneksi, $query);
  87.     while ($row = mysqli_fetch_array($result)) {
  88.         $jmlData = $row[0];
  89.     }
  90.  
  91.     return $jmlData;
  92. }
  93.  
  94. // mencari jumlah kriteria
  95. function getJumlahKriteria() {
  96.     include('config.php');
  97.     $query  = "SELECT count(*) FROM kriteria";
  98.     $result = mysqli_query($koneksi, $query);
  99.     while ($row = mysqli_fetch_array($result)) {
  100.         $jmlData = $row[0];
  101.     }
  102.  
  103.     return $jmlData;
  104. }
  105.  
  106. // menambah data kriteria / alternatif
  107. function tambahData($tabel,$nama,$nilai) {
  108.     include('config.php');
  109.  
  110.     $query  =
  111.     "INSERT INTO $tabel (nama,nilai) VALUES ('$nama', '$nilai')";
  112.  
  113.     $tambah = mysqli_query($koneksi, $query);
  114.  
  115.     if (!$tambah) {
  116.         echo "Gagal mmenambah data".$tabel;
  117.         exit();
  118.     }
  119. }
  120.  
  121. // hapus kriteria
  122. function deleteKriteria($id) {
  123.     include('config.php');
  124.  
  125.     // hapus record dari tabel kriteria
  126.     $query  = "DELETE FROM kriteria WHERE id=$id";
  127.     mysqli_query($koneksi, $query);
  128.  
  129.     // hapus record dari tabel pv_kriteria
  130.     $query  = "DELETE FROM pv_kriteria WHERE id_kriteria=$id";
  131.     mysqli_query($koneksi, $query);
  132.  
  133.     // hapus record dari tabel pv_alternatif
  134.     $query  = "DELETE FROM pv_alternatif WHERE id_kriteria=$id";
  135.     mysqli_query($koneksi, $query);
  136.  
  137.     $query  = "DELETE FROM perbandingan_kriteria WHERE kriteria1=$id OR kriteria2=$id";
  138.     mysqli_query($koneksi, $query);
  139.  
  140.     $query  = "DELETE FROM perbandingan_alternatif WHERE pembanding=$id";
  141.     mysqli_query($koneksi, $query);
  142. }
  143.  
  144. // hapus alternatif
  145. function deleteAlternatif($id) {
  146.     include('config.php');
  147.  
  148.     // hapus record dari tabel alternatif
  149.     $query  = "DELETE FROM alternatif WHERE id=$id";
  150.     mysqli_query($koneksi, $query);
  151.  
  152.     // hapus record dari tabel pv_alternatif
  153.     $query  = "DELETE FROM pv_alternatif WHERE id_alternatif=$id";
  154.     mysqli_query($koneksi, $query);
  155.  
  156.     // hapus record dari tabel ranking
  157.     $query  = "DELETE FROM ranking WHERE id_alternatif=$id";
  158.     mysqli_query($koneksi, $query);
  159.  
  160.     $query  = "DELETE FROM perbandingan_alternatif WHERE alternatif1=$id OR alternatif2=$id";
  161.     mysqli_query($koneksi, $query);
  162. }
  163.  
  164. // memasukkan nilai priority vektor kriteria
  165. function inputKriteriaPV ($id_kriteria,$pv) {
  166.     include ('config.php');
  167.  
  168.     $query = "SELECT * FROM pv_kriteria WHERE id_kriteria=$id_kriteria";
  169.     $result = mysqli_query($koneksi, $query);
  170.  
  171.     if (!$result) {
  172.         echo "Error !!!";
  173.         exit();
  174.     }
  175.  
  176.     // jika result kosong maka masukkan data baru
  177.     // jika telah ada maka diupdate
  178.     if (mysqli_num_rows($result)==0) {
  179.         $query = "INSERT INTO pv_kriteria (id_kriteria, nilai) VALUES ($id_kriteria, $pv)";
  180.     } else {
  181.         $query = "UPDATE pv_kriteria SET nilai=$pv WHERE id_kriteria=$id_kriteria";
  182.     }
  183.  
  184.  
  185.     $result = mysqli_query($koneksi, $query);
  186.     if(!$result) {
  187.         echo "Gagal memasukkan / update nilai priority vector kriteria";
  188.         exit();
  189.     }
  190.  
  191. }
  192.  
  193. // memasukkan nilai priority vektor alternatif
  194. function inputAlternatifPV ($id_alternatif,$id_kriteria,$pv) {
  195.     include ('config.php');
  196.  
  197.     $query  = "SELECT * FROM pv_alternatif WHERE id_alternatif = $id_alternatif AND id_kriteria = $id_kriteria";
  198.     $result = mysqli_query($koneksi, $query);
  199.  
  200.     if (!$result) {
  201.         echo "Error !!!";
  202.         exit();
  203.     }
  204.  
  205.     // jika result kosong maka masukkan data baru
  206.     // jika telah ada maka diupdate
  207.     if (mysqli_num_rows($result)==0) {
  208.         $query = "INSERT INTO pv_alternatif (id_alternatif,id_kriteria,nilai) VALUES ($id_alternatif,$id_kriteria,$pv)";
  209.     } else {
  210.         $query = "UPDATE pv_alternatif SET nilai=$pv WHERE id_alternatif=$id_alternatif AND id_kriteria=$id_kriteria";
  211.     }
  212.  
  213.     $result = mysqli_query($koneksi, $query);
  214.     if (!$result) {
  215.         echo "Gagal memasukkan / update nilai priority vector alternatif";
  216.         exit();
  217.     }
  218.  
  219. }
  220.  
  221.  
  222. // memasukkan bobot nilai perbandingan kriteria
  223. function inputDataPerbandinganKriteria($kriteria1,$kriteria2,$nilai) {
  224.     include('config.php');
  225.  
  226.     $id_kriteria1 = getKriteriaID($kriteria1);
  227.     $id_kriteria2 = getKriteriaID($kriteria2);
  228.  
  229.     $query  = "SELECT * FROM perbandingan_kriteria WHERE kriteria1 = $id_kriteria1 AND kriteria2 = $id_kriteria2";
  230.     $result = mysqli_query($koneksi, $query);
  231.  
  232.     if (!$result) {
  233.         echo "Error !!!";
  234.         exit();
  235.     }
  236.  
  237.     // jika result kosong maka masukkan data baru
  238.     // jika telah ada maka diupdate
  239.     if (mysqli_num_rows($result)==0) {
  240.         $query = "INSERT INTO perbandingan_kriteria (kriteria1,kriteria2,nilai) VALUES ($id_kriteria1,$id_kriteria2,$nilai)";
  241.     } else {
  242.         $query = "UPDATE perbandingan_kriteria SET nilai=$nilai WHERE kriteria1=$id_kriteria1 AND kriteria2=$id_kriteria2";
  243.     }
  244.  
  245.     $result = mysqli_query($koneksi, $query);
  246.     if (!$result) {
  247.         echo "Gagal memasukkan data perbandingan";
  248.         exit();
  249.     }
  250.  
  251. }
  252.  
  253. // memasukkan bobot nilai perbandingan alternatif
  254. function inputDataPerbandinganAlternatif($alternatif1,$alternatif2,$pembanding,$nilai) {
  255.     include('config.php');
  256.  
  257.  
  258.     $id_alternatif1 = getAlternatifID($alternatif1);
  259.     $id_alternatif2 = getAlternatifID($alternatif2);
  260.     $id_pembanding  = getKriteriaID($pembanding);
  261.  
  262.     $query  = "SELECT * FROM perbandingan_alternatif WHERE alternatif1 = $id_alternatif1 AND alternatif2 = $id_alternatif2 AND pembanding = $id_pembanding";
  263.     $result = mysqli_query($koneksi, $query);
  264.  
  265.     if (!$result) {
  266.         echo "Error !!!";
  267.         exit();
  268.     }
  269.  
  270.     // jika result kosong maka masukkan data baru
  271.     // jika telah ada maka diupdate
  272.     if (mysqli_num_rows($result)==0) {
  273.         $query = "INSERT INTO perbandingan_alternatif (alternatif1,alternatif2,pembanding,nilai) VALUES ($id_alternatif1,$id_alternatif2,$id_pembanding,$nilai)";
  274.     } else {
  275.         $query = "UPDATE perbandingan_alternatif SET nilai=$nilai WHERE alternatif1=$id_alternatif1 AND alternatif2=$id_alternatif2 AND pembanding=$id_pembanding";
  276.     }
  277.  
  278.     $result = mysqli_query($koneksi, $query);
  279.     if (!$result) {
  280.         echo "Gagal memasukkan data perbandingan";
  281.         exit();
  282.     }
  283.  
  284. }
  285.  
  286. // mencari nilai bobot perbandingan kriteria
  287. function getNilaiPerbandinganKriteria($kriteria1,$kriteria2) {
  288.     include('config.php');
  289.  
  290.     $id_kriteria1 = getKriteriaID($kriteria1);
  291.     $id_kriteria2 = getKriteriaID($kriteria2);
  292.  
  293.     $query  = "SELECT nilai FROM perbandingan_kriteria WHERE kriteria1 = $id_kriteria1 AND kriteria2 = $id_kriteria2";
  294.     $result = mysqli_query($koneksi, $query);
  295.  
  296.     if (!$result) {
  297.         echo "Error !!!";
  298.         exit();
  299.     }
  300.  
  301.     if (mysqli_num_rows($result)==0) {
  302.         $nilai = 1;
  303.     } else {
  304.         while ($row = mysqli_fetch_array($result)) {
  305.             $nilai = $row['nilai'];
  306.         }
  307.     }
  308.  
  309.     return $nilai;
  310. }
  311.  
  312. // mencari nilai bobot perbandingan alternatif
  313. function getNilaiPerbandinganAlternatif($alternatif1,$alternatif2,$pembanding) {
  314.     include('config.php');
  315.  
  316.     $id_alternatif1 = getAlternatifID($alternatif1);
  317.     $id_alternatif2 = getAlternatifID($alternatif2);
  318.     $id_pembanding  = getKriteriaID($pembanding);
  319.  
  320.     $query  = "SELECT nilai FROM perbandingan_alternatif WHERE alternatif1 = $id_alternatif1 AND alternatif2 = $id_alternatif2 AND pembanding = $id_pembanding";
  321.     $result = mysqli_query($koneksi, $query);
  322.  
  323.     if (!$result) {
  324.         echo "Error !!!";
  325.         exit();
  326.     }
  327.     if (mysqli_num_rows($result)==0) {
  328.         $nilai = 1;
  329.     } else {
  330.         while ($row = mysqli_fetch_array($result)) {
  331.             $nilai = $row['nilai'];
  332.         }
  333.     }
  334.  
  335.     return $nilai;
  336. }
  337.  
  338. // menampilkan nilai IR
  339. function getNilaiIR($jmlKriteria) {
  340.     include('config.php');
  341.     $query  = "SELECT nilai FROM ir WHERE jumlah=$jmlKriteria";
  342.     $result = mysqli_query($koneksi, $query);
  343.     while ($row = mysqli_fetch_array($result)) {
  344.         $nilaiIR = $row['nilai'];
  345.     }
  346.  
  347.     return $nilaiIR;
  348. }
  349.  
  350. // mencari Principe Eigen Vector (λ maks)
  351. function getEigenVector($matrik_a,$matrik_b,$n) {
  352.     $eigenvektor = 0;
  353.     for ($i=0; $i <= ($n-1) ; $i++) {
  354.         $eigenvektor += ($matrik_a[$i] * (($matrik_b[$i]) / $n));
  355.     }
  356.  
  357.     return $eigenvektor;
  358. }
  359.  
  360. // mencari Cons Index
  361. function getConsIndex($matrik_a,$matrik_b,$n) {
  362.     $eigenvektor = getEigenVector($matrik_a,$matrik_b,$n);
  363.     $consindex = ($eigenvektor - $n)/($n-1);
  364.  
  365.     return $consindex;
  366. }
  367.  
  368. // Mencari Consistency Ratio
  369. function getConsRatio($matrik_a,$matrik_b,$n) {
  370.     $consindex = getConsIndex($matrik_a,$matrik_b,$n);
  371.     $consratio = $consindex / getNilaiIR($n);
  372.  
  373.     return $consratio;
  374. }
  375.  
  376. // menampilkan tabel perbandingan bobot
  377. function showTabelPerbandingan($jenis,$kriteria) {
  378.     include('config.php');
  379.  
  380.     if ($kriteria == 'kriteria') {
  381.         $n = getJumlahKriteria();
  382.     } else {
  383.         $n = getJumlahAlternatif();
  384.     }
  385.  
  386.     $query = "SELECT * FROM $kriteria";
  387.     $result = mysqli_query($koneksi, $query);
  388.     if (!$result) {
  389.         echo "Error koneksi database!!!";
  390.         exit();
  391.     }
  392.  
  393.     // buat list nama pilihan
  394.     while ($row = mysqli_fetch_array($result)) {
  395.         $pilihan[] = $row['nama'];
  396.         $ininilai[] = $row['nilai'];
  397.     }
  398.  
  399.     // tampilkan tabel
  400.     ?>
  401.      <form class="ui form" action="proses.php" method="post" method="deleteall">
  402.     <table class="ui celled selectable collapsing table">
  403.         <thead>
  404.             <tr>
  405.                 <th colspan="2">pilih yang lebih penting</th>
  406.                 <th>nilai perbandingan</th>
  407.             </tr>
  408.         </thead>
  409.         <tbody>
  410.  
  411.     <?php
  412.  
  413.     //inisialisasi
  414.     $urut = 0;
  415.  
  416.     for ($x=0; $x <= ($n - 2); $x++) {
  417.         for ($y=($x+1); $y <= ($n - 1) ; $y++) {
  418.  
  419.             $urut++;
  420.  
  421.     ?>
  422.             <tr>
  423.                 <td>
  424.                     <div class="field">
  425.                         <div class="ui radio checkbox">
  426.                             <input name="pilih<?php echo $urut?>" value="1" checked="" class="hidden" type="radio">
  427.                             <label><?php echo $pilihan[$x]; ?></label>
  428.                         </div>
  429.                     </div>
  430.                 </td>
  431.                 <td>
  432.                     <div class="field">
  433.                         <div class="ui radio checkbox">
  434.                             <input name="pilih<?php echo $urut?>" value="2" class="hidden" type="radio">
  435.                             <label><?php echo $pilihan[$y]; ?></label>
  436.                         </div>
  437.                     </div>
  438.                 </td>
  439.                 <td>
  440.                     <div class="field">
  441.  
  442.     <?php
  443.     if ($kriteria == 'kriteria') {
  444.         $nilai = getNilaiPerbandinganKriteria($x,$y);
  445.     } else {
  446.         $nilai = getNilaiPerbandinganAlternatif($x,$y,($jenis-1));
  447.     }
  448.  
  449.     ?>
  450.                         <input type="text" name="bobot<?php echo $urut?>" value="<?php echo $ininilai [$y]?>" required>
  451.                     </div>
  452.                 </td>
  453.             </tr>
  454.             <?php
  455.         }
  456.     }
  457.  
  458.     ?>
  459.         </tbody>
  460.     </table>
  461.     <input type="text" name="jenis" value="<?php echo $jenis; ?>" hidden>
  462.     <br><br><input class="btn btn-primary btn-round" type="submit" name="submit" value="SUBMIT">
  463.     </form>
  464.  
  465.     <?php
  466. }
  467.  
  468. ?>
  469.  
RAW Paste Data