58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
include "../ConnectDB.php";
|
|
$q = $_REQUEST["q"];
|
|
$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
|
|
$limit = 3;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
// find the total corresponding message
|
|
$sql_count = "SELECT COUNT(*) AS count FROM restaurant WHERE name LIKE '%".$q."%'";
|
|
$result_count = $conn->query($sql_count);
|
|
$row_count = $result_count->fetch_assoc();
|
|
$count = $row_count["count"];
|
|
|
|
// calculate the total page number
|
|
$total_pages = ceil($count / $limit);
|
|
$start=microtime(true);
|
|
// search current page data
|
|
$sql = "SELECT
|
|
restaurant.restaurant_id,
|
|
restaurant.name AS rname,
|
|
cities.name AS cname,
|
|
restaurant.location,
|
|
restaurant.open_day,
|
|
restaurant.close_day,
|
|
restaurant.open_time,
|
|
restaurant.close_time,
|
|
restaurant.rating,
|
|
restaurant_style.restaurant_style_name,
|
|
restaurant.descrip,
|
|
restaurant.fee,
|
|
restaurant_style.restaurant_style_image AS image
|
|
FROM
|
|
restaurant
|
|
JOIN
|
|
cities ON restaurant.city_id = cities.id
|
|
JOIN
|
|
restaurant_style ON restaurant.restaurant_style_id = restaurant_style.restaurant_style_id
|
|
WHERE restaurant.name LIKE '%".$q."%' LIMIT $limit OFFSET $offset;";
|
|
$result = $conn->query($sql);
|
|
$end = microtime(true);
|
|
$execution_time = $end - $start;
|
|
$data = array();
|
|
if ($result->num_rows > 0) {
|
|
while($row = $result->fetch_assoc()) {
|
|
$row["image"] = base64_encode($row["image"]);
|
|
$data[] = $row;
|
|
}
|
|
} else {
|
|
$data[] = "No result";
|
|
}
|
|
|
|
echo json_encode(array("data" => $data, "total_pages" => $total_pages,"time"=>$execution_time));
|
|
?>
|
|
|
|
|
|
|
|
|