This repository has been archived on 2021-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
chie/public/search.php

59 lines
1.8 KiB
PHP
Raw Normal View History

2020-07-06 17:09:56 +00:00
<?php
require_once '../startup.php';
$title = 'Search';
$hideSearch = true;
$query = isset($_GET['q']) && is_string($_GET['q']) ? $_GET['q'] : '';
$hasQuery = !empty($query);
if($hasQuery) {
$findTopics = $pdo->prepare('
SELECT t.*
FROM `fmf_topics` AS t
WHERE t.`topic_title` LIKE CONCAT("%", LOWER(:title), "%")
');
$findTopics->bindValue('title', $query);
$topics = $findTopics->execute() ? $findTopics->fetchAll(PDO::FETCH_ASSOC) : [];
$findPosts = $pdo->prepare('
SELECT p.*, t.`topic_title`, u.`user_login`
FROM `fmf_posts` AS p
LEFT JOIN `fmf_topics` AS t
ON t.`topic_id` = p.`topic_id`
LEFT JOIN `fmf_users` AS u
ON u.`user_id` = p.`user_id`
WHERE p.`post_text` LIKE CONCAT("%", LOWER(:text), "%")
AND p.`post_type` = 0
AND p.`post_deleted` IS NULL
');
$findPosts->bindValue('text', $query);
$posts = $findPosts->execute() ? $findPosts->fetchAll(PDO::FETCH_ASSOC) : [];
}
include FMF_LAYOUT . '/header.php';
?>
<form method="get" action="" class="search-form">
<input type="search" class="search-input" name="q" value="<?=$query;?>"/>
<input type="submit" class="search-submit" value="Search"/>
</form>
<?php
if($hasQuery) {
printf('<span style="font-size: .9em;">Found %d topics and %d posts.</span>', count($topics), count($posts));
echo '<h3>Topics</h3>';
foreach($topics as $topic) {
?>
<a href="/topic/<?=$topic['topic_id'];?>"><?=htmlentities($topic['topic_title']);?></a><br/>
<?php
}
echo '<h3>Posts</h3>';
foreach($posts as $post) {
?>
<a href="/post/<?=$post['post_id'];?>">Re: <?=htmlentities($post['topic_title']);?> by <?=($post['user_login'] ?? 'Deleted User');?> #<?=$post['post_id'];?></a><br/>
<?php
}
}
include FMF_LAYOUT . '/footer.php';