mirror of
https://github.com/YGGverse/YGGo.git
synced 2025-01-08 22:07:56 +00:00
implement search results pagination
This commit is contained in:
parent
981caa3e69
commit
4ea01bf8b4
@ -7,6 +7,7 @@ error_reporting(E_ALL);
|
|||||||
|
|
||||||
// Website
|
// Website
|
||||||
define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : ''));
|
define('WEBSITE_DOMAIN', (issue($_SERVER['HTTP_HOST']) ? 'http://' . $_SERVER['HTTP_HOST'] : ''));
|
||||||
|
define('WEBSITE_PAGINATION_SEARCH_RESULTS_LIMIT', 100);
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
define('DB_NAME', 'database.sqlite');
|
define('DB_NAME', 'database.sqlite');
|
||||||
|
@ -150,9 +150,9 @@ class SQLite {
|
|||||||
return $query->rowCount();
|
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]);
|
$query->execute([$q]);
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
|
|||||||
|
|
||||||
// Filter request data
|
// Filter request data
|
||||||
$q = !empty($_GET['q']) ? Filter::url($_GET['q']) : '';
|
$q = !empty($_GET['q']) ? Filter::url($_GET['q']) : '';
|
||||||
|
$p = !empty($_GET['p']) ? (int) $_GET['p'] : 1;
|
||||||
|
|
||||||
// Crawl request
|
// Crawl request
|
||||||
if (filter_var($q, FILTER_VALIDATE_URL) && preg_match(CRAWL_URL_REGEXP, $q)) {
|
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
|
// Search request
|
||||||
if (!empty($q)) {
|
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 . '"');
|
$resultsTotal = $db->searchPagesTotal('"' . $q . '"');
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -42,7 +43,7 @@ if (!empty($q)) {
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="<?php echo _('en-US'); ?>">
|
<html lang="<?php echo _('en-US'); ?>">
|
||||||
<head>
|
<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 charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" />
|
<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>
|
<a href="<?php echo $result->url ?>"><?php echo $result->url ?></a>
|
||||||
</div>
|
</div>
|
||||||
<?php } ?>
|
<?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 { ?>
|
<?php } else { ?>
|
||||||
<div style="text-align:center">
|
<div style="text-align:center">
|
||||||
<span><?php echo sprintf(_('Total found: %s'), $resultsTotal) ?></span>
|
<span><?php echo sprintf(_('Total found: %s'), $resultsTotal) ?></span>
|
||||||
|
Loading…
Reference in New Issue
Block a user