I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance
Related
I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you?
This my code have not error... take this code with your project implement..
<ul class="pagination justify-content-end">
<?php
$sql = "SELECT * FROM json_data";
$rs_result1 = mysqli_query($connect, $sql);
$row = mysqli_num_rows($rs_result1);
//print_r($rs_result1); die();
$limit = 10;
$total_records = $row;
//$total_pages = ceil($total_records / $limit)-220;
// calculate total pages
$total_pages = ceil($row / $limit);
$prev = $page-1;
$next = $page+1;
$pagination_buttons =5;
$last_page = $total_pages;
$half = floor($pagination_buttons/2);
//echo '<ul class="pagination">';
if($page >= 5){
echo '<li>First</li>';
}
if($page < $pagination_buttons AND ($last_page == $pagination_buttons OR $last_page > $pagination_buttons)){
for($i=1; $i<=$pagination_buttons; $i++){
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
if($last_page > $pagination_buttons){
echo '<li>Next</li>';
}
}
else if($page >= $pagination_buttons AND $last_page > $pagination_buttons){
if(($page+$half) >= $last_page){
echo '<li>Previous</li>';
for ($i=($last_page-$pagination_buttons)+1; $i<=$last_page; $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
}
else if(($page+$half) < $last_page){
echo '<li>Previous</li>';
for ($i=($page-$half); $i<=($page+$half); $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
echo '<li>Next</li>';
}
}
if($page != $total_pages && $total_pages >= 6){
echo '<li>Last</li>';
} ?>
i solve it my code in pagination in php with mysql.. and hope may help this code your pagination project...
I coded pagination code for my site in php and mysql.. I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you? This my code have not error... take this code with your project implement..
Help me, i was writing my code, all done succesfully but my problem is when the "..." (separator) for my curerent page and last page showed after the last page, not between my current page and my last page
how can that possible ?
this is my script for showing the page :
<?php
$paging2 = mysqli_query($koneksi,"select * from laporan where seksi='{$seksi}'");
$jmldata = mysqli_num_rows($paging2);
$jmlhalaman = ceil($jmldata/$batas);
echo "<ul class='pagination'>";
if ($halaman !== 1){
echo "<li><a href='index.php?load=table_laporan&halaman=1'>First</a></li>";
}
if ($halaman > 1){
echo "<li><a href='index.php?load=table_laporan&halaman=".($halaman-1)."'>Previous</a></li>";
}
for($i =1; $i <= $jmlhalaman; $i++){
if ((($i >= $halaman - 3) && ($i <= $halaman + 3)) || ($i == 1) || ($i ==$jmlhalaman))
{
if ($i == $halaman){
$class="active";
} else{
$class="disable";
} //echo "<b>$i</b>";
echo "<li class='$class'>".$i."<span class='sr-only'>(current)</span></li>";
if($i==$jmlhalaman && $halaman <= $jmlhalaman-5) echo "<li><a>...</a></li>";
if($i==1 && $halaman >= 6) echo "<li><a>...</a></li>";
}
}
if ($halaman >= 1){
echo "<li><a href=index.php?load=table_laporan&halaman=". ($halaman+1)."'>Next</a></li>";
} if ($halaman >= 1){
echo "<li><a href='index.php?load=table_laporan&halaman=". ($jmlhalaman)."'>Last</a></li>";
}
echo "</ul>";
echo "<p>Total Data : <b>$jmldata</b> Data</p>";
?>
This code only shows one of the pagination links as active if I click on it first. But on page load I want number "1" to be active by default.
if(!empty($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
for ($page = 1; $page <= $number_of_pages; $page++) {
if ($_GET['page'] == $page) {
echo "<li class='pagination__page--active'><span>{$page}</span></li>";
}
else {
echo "<li><a href='product-category/$page_url/page/{$page}'>{$page}</a></li>";
}
}
Is your problem, that only on page 1 it doesn't show the correct one as active?
If so, your problem is that you are probably not sending a 'page' with your GET request. The easiest way to solve this is:
$curr_page = (isset($_GET['page'])) ? $_GET['page'] : 1;
for ($page = 1; $page <= $number_of_pages; $page++) {
if ($curr_page == $page) {
echo "<li class='pagination__page--active'><span>{$page}</span></li>";
}
else {
echo "<li><a href='product-category/$page_url/page/{$page}'>{$page}</a></li>";
}
}
How to customize the pagination page number below?
Current:
and what I want is:
1 2 3 4 5 Next Last Page
Here is the PHP code:
for($i=1; $i<=$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
Any clue how to do this?
Could try this:
$Num_Pages = 70;
//$Page = $_GET['Page'];
$Page = 12;
$from = $Page - ($Page % 5) + 1;
for($i=$from; $i<$from+5 && $i<$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
$next_page = $Page+1;
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>Last Page</a> ]";
Replace the $Page = 12 with the $_GET to get the page dynamically.
Haven't really tested it.
$start_page = intval($Page / 5) *5 + 1;
$to_page = $start_page + 4;
$is_last_group = ($start_page + 5)> $Num_Pages);
if (is_last_group)
$to_page = $Num_Pages;
for($i = $start_page; $i <= to_page; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($page < $Num_Pages){
$next_page = $page + 1;
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>last</a> ]";
}
I am working on a php ajax pagination.
The problem is that it wont ad the class="highlightActivePage" proper.
If I click on 1 - 2 - 3 , it works fine and highlight the number 3 if im on page 3.
But soon as I click on page 4 it highlights page 5 instead, etc.
Not sure if my for loop is wrong, but here us my code:
if (paginationHTML == "")
{
paginationHTML += "<ul>";
if (adResultsData.show_first_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(1);'>First</a></li>";
}
if (adResultsData.show_previous_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page - 1) + ");'>Prev</a></li>";
}
for (var i = 0; i < adResultsData.pages.length; i++)
{
if (adResultsData.current_page == (i + 1))
{
paginationHTML += "<li><a href='#' class='highlightActivePage' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
}
else
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.pages[i] + ");'>" + adResultsData.pages[i] + "</a></li>";
}
}
if (adResultsData.show_next_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + (adResultsData.current_page + 1) + ");'>Next</a></li>";
}
if (adResultsData.show_last_text == 1)
{
paginationHTML += "<li><a href='#' onclick='fetchResults(" + adResultsData.number_of_pages + ");'>last</a></li>";
}
paginationHTML += "</ul>";
paginationHTML += pageSpan.innerHTML = "<br>Page " + adResultsData.current_page + " of " + adResultsData.number_of_pages;
}
php
$numberOfPages = $results['pages'];
$currentPage = $results['currentPage'];
if ($currentPage != 1 && $currentPage != 2)
{$showFirst = 1;}
else $showFirst = 0;
if ($currentPage != 1)
{$showPrevious = 1;}
else $showPrevious = 0;
if ($currentPage != $numberOfPages)
{$showNext = 1;}
else $showNext = 0;
if ($currentPage != $numberOfPages && $currentPage != ($numberOfPages - 1))
{$showLast = 1;}
else $showLast = 0;
if ($currentPage <= 5 && $numberOfPages <= 5 || $numberOfPages <= 5)
{$startingPage = 1;}
else if ($currentPage == 1 || $currentPage == 2)
{$startingPage = 1;}
else
{$startingPage = $currentPage - 2;}
$pageNumbers = [];
for ($i = $startingPage; $i < ($startingPage + 5) && $i <= $numberOfPages; $i++)
{
$pageNumbers[] = $i;
}
$pagesString = implode(", ", $pageNumbers);
$listingsString = implode(", ", $listingsArray);
$jsonString = <<< END
{
"resultsTotal" : $numberOfResults,
"listings" : [$listingsString],
"number_of_pages" : $numberOfPages,
"current_page" : $currentPage,
Thanks!