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