Newer
Older
<?php declare(strict_types=1);
require_once 'Page.php';
require_once 'util/constants.php';
class NewsFetch extends Page
{
private const TITLE = "title";
private const SEARCH_WORD = "searchWord";
private string $searchWord;
protected function __construct()
{
parent::__construct();
$this->searchWord = "";
}
public function __destruct()
{
parent::__destruct();
}
protected function getViewData():array
{
$data = array();
if (empty(trim($this->searchWord)))
return $data;
$stmt = $this->database->prepare("SELECT n.id, n.title FROM news n WHERE LOWER(n.title) LIKE LOWER(?) LIMIT 5");
$stmt->bind_param("s", $likeSearchWord);
$stmt->execute();
$resultSet = $stmt->get_result();
while($row = $resultSet->fetch_assoc()) {
$data[] = array(self::ID => $id, self::TITLE => $title);
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
}
return $data;
}
protected function generateView():void
{
$data = $this->getViewData();
header("Content-type: application/json");
$json = json_encode($data);
echo $json;
}
protected function processReceivedData():void
{
parent::processReceivedData();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (isset($_GET[self::SEARCH_WORD])) {
$this->searchWord = $_GET[self::SEARCH_WORD];
}
}
}
public static function main():void
{
try {
$page = new NewsFetch();
$page->processReceivedData();
$page->generateView();
} catch (Exception $e) {
header("Content-type: application/json");
echo $e->getMessage();
}
}
}
NewsFetch::main();