-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.php
112 lines (83 loc) · 2.61 KB
/
function.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
<?php
$conn = mysqli_connect("localhost", "root", "", "db_property");
function read($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function create($data) {
global $conn;
$avatar = upload();
if (!$avatar) {
return false;
}
$name = htmlspecialchars($data["name"]);
$job = htmlspecialchars($data["job"]);
$testimonial = htmlspecialchars($data["testimonial"]);
$query = "INSERT INTO tbl_testimonial VALUES (null, '$avatar', '$name', '$job', '$testimonial')";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function delete($id) {
global $conn;
$query = "DELETE FROM tbl_testimonial WHERE id = $id";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function update($data) {
global $conn;
$id = $data["id"];
$avatar = htmlspecialchars($data["avatar"]);
$oldAvatar = htmlspecialchars($data['old-avatar']);
if ($_FILES["avatar"]["error"] === 4) {
$avatar = $oldAvatar;
} else {
$avatar = upload();
}
$name = htmlspecialchars($data["name"]);
$job = htmlspecialchars($data["job"]);
$testimonial = htmlspecialchars($data["testimonial"]);
$query = "UPDATE tbl_testimonial SET avatar = '$avatar', name = '$name', job = '$job', testimonial = '$testimonial' WHERE id = $id";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
function search($keyword) {
$query = "SELECT * FROM tbl_property WHERE place LIKE '%$keyword%'";
return read($query);
}
function upload() {
$fileName = $_FILES["avatar"]["name"];
$fileSize = $_FILES["avatar"]["size"];
$tmpName = $_FILES["avatar"]["tmp_name"];
$extension = ["jpg", "jpeg", "png", "webp"];
$pictureExt = explode('.', $fileName);
$pictureExt = strtolower(end($pictureExt));
// Cek jika extensi file tidak didukung
if (!in_array($pictureExt, $extension)) {
echo "
<script>
alert('Invalid image extension!');
</script>
";
return false;
}
// Cek jika ukuran file terlalu besar
if ($fileSize > 10000000) {
echo "
<script>
alert('Image size is too large!');
</script>
";
return false;
}
// Ambil nama file dan simpang kedalama folder files
$newFileName = uniqid();
$newFileName .= ".";
$newFileName .= $pictureExt;
move_uploaded_file($tmpName, './files/' . $newFileName);
return $newFileName;
}