This is my pagination:
if (isset(filter_input_array(INPUT_GET)["page"])) {
$page= filter_input(INPUT_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS);
} else {
$page= 1;
}
$request= "";
if (filter_input_array(INPUT_POST)) {
$request= filter_input(INPUT_POST, 'request', FILTER_SANITIZE_SPECIAL_CHARS);
} else if (isset(filter_input_array(INPUT_GET)["request"])) {
$request= filter_input(INPUT_GET, 'request', FILTER_SANITIZE_SPECIAL_CHARS);
}
Database:
$sql = DB::getInstance()->query("SELECT * FROM users LIMIT " . (($page* 50) - 50) . ",50");
index.php
<nav role="navigation">
<ul class="cd-pagination custom-buttons">
<li class="arrows"><a href="index.php?page=<?php
if ($page > 1) {
$prev= $page - 1;
echo escape($prev);
} else {
echo 1;
}?>&request=<?php echo escape($request) ?>">Prev</a></li>
<li class="arrows">
<a href="index.php?page=<?php echo escape(++$page); ?>&request=
<?php echo escape($request) ?>">Next</a></li>
</ul>
</nav>
It works fine like this, but I would like it show number of pages not just prev/next. I looked for soulution on internet but nothing worked...
Query without LIMIT
:
SELECT * FROM users;
Get the number of rows from this query. I can't see your DB class so don't know what implementation you are using. Check out the documentation for num_rows
Then you need to do: ceil($numRows / $numberOfResultsPerPage);
to get the number of pages.
if (isset($_GET["p"])) {
$p = $_GET["p"];
} else {
$p = 1;
}
$per_page = 50
$from = ($p-1) * $per_page;
$sql = DB::getInstance()->query("SELECT COUNT(*) as total FROM users LIMIT $from,$per_page");
Then with the total you can use the ceil function to get the number of pages
$total_pages = ceil($total / $per_page);
In the end forech on $total_pages to display each page as <a href="?p=$i"></a>
element.
Hope it help !