This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranking-saw.php
270 lines (236 loc) · 7.55 KB
/
ranking-saw.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
<?php
/* ---------------------------------------------
* SPK SAW
* Author: Wawan Edan
* ------------------------------------------- */
/* ---------------------------------------------
* Konek ke database & load fungsi-fungsi
* ------------------------------------------- */
require_once('includes/init.php');
/* ---------------------------------------------
* Load Header
* ------------------------------------------- */
$judul_page = 'Perankingan Menggunakan Metode SAW';
require_once('template-parts/header.php');
/* ---------------------------------------------
* Set jumlah digit di belakang koma
* ------------------------------------------- */
$digit = 4;
/* ---------------------------------------------
* Fetch semua kriteria
* ------------------------------------------- */
$query = $pdo->prepare('SELECT id_kriteria, nama, type, bobot
FROM kriteria
WHERE kyu_id = ' . $_GET['kyu'] . '
ORDER BY urutan_order ASC');
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$kriterias = $query->fetchAll();
/* ---------------------------------------------
* Fetch semua kenshin (alternatif)
* ------------------------------------------- */
$query2 = $pdo->prepare('SELECT id_kenshin, nama_kenshin FROM kenshin WHERE kyu_id = ' . $_GET['kyu']);
$query2->execute();
$query2->setFetchMode(PDO::FETCH_ASSOC);
$kenshins = $query2->fetchAll();
/* >>> STEP 1 ===================================
* Matrix Keputusan (X)
* ------------------------------------------- */
$matriks_x = array();
$list_kriteria = array();
foreach($kriterias as $kriteria):
$list_kriteria[$kriteria['id_kriteria']] = $kriteria;
foreach($kenshins as $kenshin):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
// Fetch nilai dari db
$query3 = $pdo->prepare('SELECT nilai FROM nilai_kenshin
WHERE id_kenshin = :id_kenshin AND id_kriteria = :id_kriteria');
$query3->execute(array(
'id_kenshin' => $id_kenshin,
'id_kriteria' => $id_kriteria,
));
$query3->setFetchMode(PDO::FETCH_ASSOC);
if($nilai_kenshin = $query3->fetch()) {
// Jika ada nilai kriterianya
$matriks_x[$id_kriteria][$id_kenshin] = $nilai_kenshin['nilai'];
} else {
$matriks_x[$id_kriteria][$id_kenshin] = 0;
}
endforeach;
endforeach;
/* >>> STEP 3 ===================================
* Matriks Ternormalisasi (R)
* ------------------------------------------- */
$matriks_r = array();
foreach($matriks_x as $id_kriteria => $nilai_kenshins):
$tipe = $list_kriteria[$id_kriteria]['type'];
foreach($nilai_kenshins as $id_alternatif => $nilai) {
if($tipe == 'c1') {
$nilai_normal = $nilai / max($nilai_kenshins);
} elseif($tipe == 'c2') {
$nilai_normal = min($nilai_kenshins) / $nilai;
} elseif($tipe == 'c3') {
$nilai_normal = min($nilai_kenshins) / $nilai;
} elseif($tipe == 'c4') {
$nilai_normal = min($nilai_kenshins) / $nilai;
} elseif($tipe == 'c5') {
$nilai_normal = min($nilai_kenshins) / $nilai;
}
$matriks_r[$id_kriteria][$id_alternatif] = $nilai_normal;
}
endforeach;
/* >>> STEP 4 ================================
* Perangkingan
* ------------------------------------------- */
$ranks = array();
foreach($kenshins as $kenshin):
$total_nilai = 0;
foreach($list_kriteria as $kriteria) {
$bobot = $kriteria['bobot'];
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
$nilai_r = $matriks_r[$id_kriteria][$id_kenshin];
$total_nilai = $total_nilai + ($bobot * $nilai_r);
}
$ranks[$kenshin['id_kenshin']]['id_kenshin'] = $kenshin['id_kenshin'];
$ranks[$kenshin['id_kenshin']]['nama_kenshin'] = $kenshin['nama_kenshin'];
$ranks[$kenshin['id_kenshin']]['nilai'] = $total_nilai;
endforeach;
?>
<div class="main-content-row">
<div class="container clearfix">
<div class="main-content main-content-full the-content">
<h1><?php echo $judul_page; ?></h1>
<!-- STEP 1. Matriks Keputusan(X) ==================== -->
<h3>Step 1: Matriks Keputusan (X) [Merupakan Nilai dan Bobot Kenshi]</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr class="super-top">
<th rowspan="2" class="super-top-left">Nama Kenshi</th>
<th colspan="<?php echo count($kriterias); ?>">Nilai per-Kriteria</th>
</tr>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<?php
foreach($kriterias as $kriteria):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
echo '<td>';
echo $matriks_x[$id_kriteria][$id_kenshin];
echo '</td>';
endforeach;
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- STEP 2. Bobot Preferensi (W) ==================== -->
<h3>Step 2: Bobot Preferensi (W)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th>Nama Kriteria</th>
<th>Type</th>
<th>Bobot (W)</th>
</tr>
</thead>
<tbody>
<?php foreach($kriterias as $hasil): ?>
<tr>
<td><?php echo $hasil['nama']; ?></td>
<td>
<?php
if($hasil['type'] == 'c1') {
echo 'C-1';
} elseif($hasil['type'] == 'c2') {
echo 'C-2';
} elseif($hasil['type'] == 'c3') {
echo 'C-3';
} elseif($hasil['type'] == 'c4') {
echo 'C-4';
} elseif($hasil['type'] == 'c5') {
echo 'C-5';
}
?>
</td>
<td><?php echo $hasil['bobot']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 3: Matriks Ternormalisasi (R) ==================== -->
<h3>Step 3: Matriks Ternormalisasi (R)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr class="super-top">
<th rowspan="2" class="super-top-left">Nama Kenshin</th>
<th colspan="<?php echo count($kriterias); ?>">Kriteria</th>
</tr>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<?php
foreach($kriterias as $kriteria):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
echo '<td>';
echo round($matriks_r[$id_kriteria][$id_kenshin], $digit);
echo '</td>';
endforeach;
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 4: Perangkingan ==================== -->
<?php
$sorted_ranks = $ranks;
// Sorting
if(function_exists('array_multisort')):
$nama_kenshin = array();
$nilai = array();
foreach ($sorted_ranks as $key => $row) {
$nama_kenshin[$key] = $row['nama_kenshin'];
$nilai[$key] = $row['nilai'];
}
array_multisort($nilai, SORT_DESC, $nama_kenshin, SORT_ASC, $sorted_ranks);
endif;
?>
<h3>Step 4: Perangkingan (V)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th class="super-top-left">Nama Kenshin</th>
<th>Ranking</th>
</tr>
</thead>
<tbody>
<?php foreach($sorted_ranks as $kenshin ): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<td><?php echo round($kenshin['nilai'], $digit); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div><!-- .container -->
</div><!-- .main-content-row -->
<?php
require_once('template-parts/footer.php');