controller.php 1.58 KB
Newer Older
1
<?php
2
require_once('view.php');
3

4 5 6 7 8
class resultController
{
    public static function showResultController(array $params)
    {
        if (isset($_COOKIE["login"])) {
9 10
            $token = $_COOKIE["login"];
            $username = getUsername($token);
11
        } else {
12 13
            $username = "NONE";
        }
14
        $text = filter_var($params["search_text"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
Ranindya Paramitha's avatar
Ranindya Paramitha committed
15
        $result_data = self::getResultData($text);
16 17 18

        $params = ['username' => $username];
        $params["item"] = $result_data;
Ranindya Paramitha's avatar
Ranindya Paramitha committed
19
        searchResultView::viewResult($params);
20
    }
21 22
    private static function getResultData($search_text)
    {
23
        $conn = connect_to_mysql();
24 25 26 27 28 29 30 31 32 33 34 35
        if ($conn !== NULL) {
            $sql_query = "SELECT T1.ID as ID, Judul, Pengarang, Deskripsi, avg(rating) as avg_rating, count(book_id) as count
                          from ((SELECT * FROM books where Judul LIKE CONCAT('%',?,'%')) as T1 left join book_order on (T1.ID = book_order.book_id))
                                left join review on (review.order_id = book_order.Nomor_Order) group by T1.ID";

            if ($stmt = $conn->prepare($sql_query)) {
                $stmt->bind_param("s", $search_text);
                $stmt->execute();
                $result = $stmt->get_result();
                $stmt->close();
            }

36
            $conn->close();
37
            if ($result != NULL) {
38
                return $result;
39
            } else {
40 41
                return 0;
            }
42
        } else {
43 44 45 46
            return 0;
        }
    }
}