Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Pagination in index and search #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions public_html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
require_once '../resources/config.php';
require_once(LIBRARIES_PATH . "/Prismic.php");

$page = isset($_GET['page']) ? $_GET['page'] : 1;

try {
$ctx = Prismic::context();
$documents = $ctx->getApi()->forms()->everything->ref($ctx->getRef())->submit();
$documents = $ctx->getApi()->forms()->everything->page($page)->ref($ctx->getRef())->submit();
} catch (Guzzle\Http\Exception\BadResponseException $e) {
Prismic::handlePrismicException($e);
}
Expand All @@ -16,7 +18,7 @@
require_once(TEMPLATES_PATH . "/header.php");
?>

<form action="<?php echo Routes::search($ctx->getRef()); ?>" method="POST">
<form action="<?php echo Routes::search($ctx->getRef()); ?>" method="get">
<input type="text" name="q" value="">
<input type="submit" value="Search">
</form>
Expand Down Expand Up @@ -44,4 +46,6 @@
</ul>

<?php
$urlWithoutPagination = Routes::index();
require_once(TEMPLATES_PATH . "/pagination.php");
require_once(TEMPLATES_PATH . "/footer.php");
12 changes: 9 additions & 3 deletions public_html/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

try {
$ctx = Prismic::context();
$maybeQuery = isset($_POST['q']) ? $_POST['q'] : '';
$maybeQuery = isset($_GET['q']) ? $_GET['q'] : '';
$q = '[[:d = fulltext(document, "' . $maybeQuery . '")]]';
$documents = $ctx->getApi()->forms()->everything->query($q)->ref($ctx->getRef())->submit();
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$documents = $ctx->getApi()->forms()->everything->query($q)->page($page)->ref($ctx->getRef())->submit();
} catch (Guzzle\Http\Exception\BadResponseException $e) {
Prismic::handlePrismicException($e);
}
Expand Down Expand Up @@ -36,5 +37,10 @@
</ul>

<p>
<a href="<?php echo Routes::index($ctx->ref) ?>">Back to home</a>
<a href="<?php echo Routes::index() ?>">Back to home</a>
</p>

<?php
$urlWithoutPagination = Routes::search($maybeQuery);
require_once(TEMPLATES_PATH . "/pagination.php");
require_once(TEMPLATES_PATH . "/footer.php");
6 changes: 4 additions & 2 deletions resources/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public static function detail($id, $slug, $maybeRef=null)
return Routes::baseUrl() . '/detail.php?' . $queryString;
}

public static function search($maybeRef=null)
public static function search($q, $maybeRef=null)
{
$parameters = array();
$parameters = array(
"q" => $q
);
if (isset($maybeRef)) {
$parameters['ref'] = $maybeRef;
}
Expand Down
48 changes: 48 additions & 0 deletions resources/templates/pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
$urlWithoutPagination = rtrim($urlWithoutPagination, '?');
$urlHasParameters = parse_url($urlWithoutPagination, PHP_URL_QUERY);
if($urlHasParameters)
{
$urlWithoutPagination .= '&page=';
}
else
{
$urlWithoutPagination .= '?page=';
}
?>

<?php if($documents->getTotalPages()>1) { ?>

<hr>

<ul>

<?php if($documents->getPrevPage()) { ?>
<li>
<a href="<?php echo $urlWithoutPagination . ($documents->getPage() - 1) ?>">
Previous
</a>
</li>
<?php } ?>

<?php for($i = 1; $i <= $documents->getTotalPages(); $i++) { ?>
<li>
<?php
if($documents->getPage() == $i) echo $i;
else {
?>
<a href="<?php echo $urlWithoutPagination . $i ?>"><?php echo $i ?></a>
<?php } ?>
</li>
<?php } ?>

<?php if($documents->getNextPage()) { ?>
<li>
<a href="<?php echo $urlWithoutPagination . ($documents->getPage() + 1) ?>">
Next
</a>
</li>
<?php } ?>

</ul>
<?php } ?>