mirror of
https://github.com/YGGverse/YGGo.git
synced 2025-01-24 13:34:25 +00:00
implement search results pagination
This commit is contained in:
parent
981caa3e69
commit
4ea01bf8b4
@ -7,6 +7,7 @@ error_reporting(E_ALL);
|
||||
|
||||
// Website
|
||||
define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : ''));
|
||||
define('WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT', 100);
|
||||
|
||||
// Database
|
||||
define('DB_NAME', 'database.sqlite');
|
||||
|
@ -150,9 +150,9 @@ class SQLite {
|
||||
return $query->rowCount();
|
||||
}
|
||||
|
||||
public function searchPages(string $q) {
|
||||
public function searchPages(string $q, int $start = 0, int $limit = 100) {
|
||||
|
||||
$query = $this->_db->prepare('SELECT `title`, `description`, `url` FROM `ftsPage` WHERE `data` MATCH ? ORDER BY `rank`');
|
||||
$query = $this->_db->prepare('SELECT `title`, `description`, `url` FROM `ftsPage` WHERE `data` MATCH ? ORDER BY `rank` LIMIT ' . (int) $start . ',' . (int) $limit);
|
||||
|
||||
$query->execute([$q]);
|
||||
|
||||
|
@ -18,6 +18,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
|
||||
|
||||
// Filter request data
|
||||
$q = !empty($_GET['q']) ? Filter::url($_GET['q']) : '';
|
||||
$p = !empty($_GET['p']) ? (int) $_GET['p'] : 1;
|
||||
|
||||
// Crawl request
|
||||
if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) {
|
||||
@ -28,7 +29,7 @@ if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) {
|
||||
// Search request
|
||||
if (!empty($q)) {
|
||||
|
||||
$results = $db->searchPages('"' . $q . '"');
|
||||
$results = $db->searchPages('"' . $q . '"', $p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT - WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT, WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT);
|
||||
$resultsTotal = $db->searchPagesTotal('"' . $q . '"');
|
||||
|
||||
} else {
|
||||
@ -42,7 +43,7 @@ if (!empty($q)) {
|
||||
<!DOCTYPE html>
|
||||
<html lang="<?php echo _('en-US'); ?>">
|
||||
<head>
|
||||
<title><?php echo sprintf(_('%s - YGGo!'), htmlentities($q)) ?></title>
|
||||
<title><?php echo sprintf(_('%s - #%s - YGGo!'), htmlentities($q), $p) ?></title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" />
|
||||
@ -189,6 +190,11 @@ if (!empty($q)) {
|
||||
<a href="<?php echo $result->url ?>"><?php echo $result->url ?></a>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php if ($p * WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT < $resultsTotal) { ?>
|
||||
<div>
|
||||
<a href="<?php echo WEBSITE_DOMAIN; ?>/search.php?q=<?php echo urlencode(htmlentities($q)) ?>&p=<?php echo $p + 1 ?>"><?php echo _('Next page') ?></a>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php } else { ?>
|
||||
<div style="text-align:center">
|
||||
<span><?php echo sprintf(_('Total found: %s'), $resultsTotal) ?></span>
|
||||
|
Loading…
x
Reference in New Issue
Block a user