Last active
August 14, 2020 00:38
-
-
Save silasrm/71b3ce40a287236f2300a3266cd18d1b to your computer and use it in GitHub Desktop.
Paginação com PHP e MySQL puro com slider com padrõa bootstra 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$currentPage = $_SERVER["PHP_SELF"]; | |
$maxRows = 15; | |
$pageNum = 1; | |
if (isset($_GET['page'])) { | |
$pageNum = $_GET['page']; | |
} | |
$startRow = ($pageNum - 1) * $maxRows; | |
mysql_select_db($database, $conn); | |
$query = "SELECT * FROM noticias ORDER BY `data` DESC"; | |
$query_limit = sprintf("%s LIMIT %d, %d", $query, $startRow, $maxRows); | |
$noticias = mysql_query($query_limit, $conn) or die(mysql_error()); | |
$row = mysql_fetch_assoc($noticias); | |
// Count | |
$queryCount = "SELECT COUNT(*) as total FROM noticias"; | |
$resultCount = mysql_query($queryCount, $conn); | |
$totalRows = mysql_fetch_array($resultCount)[0]; | |
$totalPages = ceil($totalRows / $maxRows); | |
$queryString = ""; | |
if (!empty($_SERVER['QUERY_STRING'])) { | |
$params = explode("&", $_SERVER['QUERY_STRING']); | |
$newParams = array(); | |
foreach ($params as $param) { | |
if (stristr($param, "page") == false) { | |
array_push($newParams, $param); | |
} | |
} | |
if (count($newParams) != 0) { | |
$queryString = "&" . htmlentities(implode("&", $newParams)); | |
} | |
} | |
?> | |
<h1>Título</h1> | |
<hr /> | |
<h2>Listagem aqui</h2> | |
<?php if($totalPages > 1): ?> | |
<nav> | |
<ul class="pagination"> | |
<!--{{-- Previous Page Link --}}--> | |
<?php if($pageNum <= 1): ?> | |
<li class="page-item disabled" aria-disabled="true"> | |
<span class="page-link">Anterior</span> | |
</li> | |
<?php else: ?> | |
<li class="page-item"> | |
<a class="page-link" href="<?php printf("%s?page=%d%s", $currentPage, max(0, $pageNum - 1), $queryString); ?>" rel="prev"> | |
Anterior | |
</a> | |
</li> | |
<?php endif; ?> | |
<?php | |
$pageAdjacents_noticias = 2; | |
if ($totalPages_noticias <= 6) { | |
for ($counter = 1; $counter <= $totalPages_noticias; $counter++) { | |
if ($counter == $pageNum_noticias) { | |
echo "<li class=\"page-item active\"><span class=\"page-link\">$counter</span></li>"; | |
} else { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $counter, $queryString_noticias) . "\">" | |
. " " . $counter | |
. " </a>" | |
. "</li>"; | |
} | |
} | |
} elseif ($totalPages_noticias > 6) { | |
if ($pageNum_noticias <= 2) { | |
for ($counter = 1; $counter < 3; $counter++) { | |
if ($counter == $pageNum_noticias) { | |
echo "<li class=\"page-item active\"><span class=\"page-link\">$counter</span></li>"; | |
} else { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $counter, $queryString_noticias) . "\">" | |
. " " . $counter | |
. " </a>" | |
. "</li>"; | |
} | |
} | |
echo "<li class=\"page-item disabled\"><span class=\"page-link\">...</span></li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $totalPages_noticias - 1, $queryString_noticias) . "\">" | |
. " " . ($totalPages_noticias - 1) | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $totalPages_noticias, $queryString_noticias) . "\">" | |
. " " . $totalPages_noticias | |
. " </a>" | |
. "</li>"; | |
} elseif ($pageNum_noticias > 2 && $pageNum_noticias < $totalPages_noticias - 2) { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, 1, $queryString_noticias) . "\">" | |
. " " . 1 | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, 2, $queryString_noticias) . "\">" | |
. " " . 2 | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item disabled\"><span class=\"page-link\">...</span></li>"; | |
for ($counter = $pageNum_noticias - $pageAdjacents_noticias; $counter <= $pageNum_noticias + $pageAdjacents_noticias; $counter++) { | |
if ($counter == $pageNum_noticias) { | |
echo "<li class=\"page-item active\"><span class=\"page-link\">$counter</span></li>"; | |
} else { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $counter, $queryString_noticias) . "\">" | |
. " " . $counter | |
. " </a>" | |
. "</li>"; | |
} | |
} | |
echo "<li class=\"page-item\"><span class=\"page-link\">...</span></li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $totalPages_noticias - 1, $queryString_noticias) . "\">" | |
. " " . ($totalPages_noticias - 1) | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $totalPages_noticias, $queryString_noticias) . "\">" | |
. " " . $totalPages_noticias | |
. " </a>" | |
. "</li>"; | |
} else { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, 1, $queryString_noticias) . "\">" | |
. " " . 1 | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, 2, $queryString_noticias) . "\">" | |
. " " . 2 | |
. " </a>" | |
. "</li>"; | |
echo "<li class=\"page-item disabled\"><span class=\"page-link\">...</span></li>"; | |
for ($counter = $totalPages_noticias - 2; $counter <= $totalPages_noticias; $counter++) { | |
if ($counter == $pageNum_noticias) { | |
echo "<li class=\"page-item active\"><span class=\"page-link\">$counter</span></li>"; | |
} else { | |
echo "<li class=\"page-item\">" | |
. " <a class=\"page-link\" href=\"" . sprintf("%s?page=%d%s", $currentPage, $counter, $queryString_noticias) . "\">" | |
. " " . $counter | |
. " </a>" | |
. "</li>"; | |
} | |
} | |
} | |
} | |
?> | |
<!--{{-- Next Page Link --}}--> | |
<?php if($totalPages > $pageNum): ?> | |
<li class="page-item"> | |
<a class="page-link" href="<?php printf("%s?page=%d%s", $currentPage, min($totalPages, $pageNum + 1), $queryString); ?>" rel="next"> | |
Próxima | |
</a> | |
</li> | |
<?php else: ?> | |
<li class="page-item disabled" aria-disabled="true"> | |
<span class="page-link">Próxima</span> | |
</li> | |
<?php endif; ?> | |
</ul> | |
</nav> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment