Repository navigation
Expand file tree
/
Copy pathlatihan7.php
More file actions
96 lines (83 loc) · 2.07 KB
/
Copy pathlatihan7.php
File metadata and controls
96 lines (83 loc) · 2.07 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="latihan7.php" method="POST">
Input : <input type="number" name="isi">
<input type="submit" name="cari" value="CARI">
<input type="submit" name="asc" value="A-Z">
<input type="submit" name="desc" value="Z-A">
<br>
<br>
</form>
<?php
$angka=array("20","90","50","30","40","10","70","80","100","60");
for ($i=0; $i < count($angka) ; $i++) {
echo 'Index ke-'.$i.' = '.$angka[$i];
echo '<br>';
}
if(isset($_POST['cari'])){
$cari = $_POST['isi'];
echo '<br>';
echo 'Hasil Cari : ';
for ($i=0; $i < count($angka) ; $i++) {
if($cari==$angka[$i]){
echo 'Angka '.$cari.' berada di Index ke-'.$i;
}
}
echo '<br>';
}
if(isset($_POST['asc'])){
echo '<br>';
echo 'Hasil Sorting A-Z';
echo '<br>';
$angka = asc($angka);
echo $angka;
}
if(isset($_POST['desc'])){
echo '<br>';
echo 'Hasil Sorting A-Z';
echo '<br>';
$angka = desc($angka);
echo $angka;
}
function asc($nilai){
$p = count($nilai);
for ($i=0; $i < $p; $i++) {
for ($j=0; $j < $p-1-$i; $j++) {
if($nilai[$j]>$nilai[$j+1]){
$sort = $nilai[$j];
$nilai[$j] = $nilai[$j+1];
$nilai[$j+1] = $sort;
}
}
}
for ($s=0; $s < $p; $s++) {
echo 'Index ke-'.$s.' = '.$nilai[$s];
echo '<br>';
}
}
function desc($nilai){
$p = count($nilai);
for ($i=0; $i < $p; $i++) {
for ($j=0; $j < $p-1-$i; $j++) {
if($nilai[$j] < $nilai[$j+1]){
$sort = $nilai[$j];
$nilai[$j] = $nilai[$j+1];
$nilai[$j+1] = $sort;
}
}
}
for ($s=0; $s < $p; $s++) {
echo 'Index ke-'.$s.' = '.$nilai[$s];
echo '<br>';
}
}
?>
</body>
</html>