How to handle the ajax pagination in php? - php

I have counted the total results and designed the pagination links. onclicking i am passsing the number value to php file. how to handle $page variable in ajax response. how to show previous 1 2 ... 100 next links like the pagination functionality. I have tried with the following functionality
$page = 1;
$total = 10;
$limit = 20;
$total_pages = ceil($total / $limit);
if (isset($_GET["page"] ))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
<ul class="pagination">
<li class="association_page active">1</li>
<li class="association_page">2</li>
<li class="association_page">3</li>
</ul>
echo "<ul class='pagination'>";
if ($page > 1) {
echo "<li class='association_page'><a href='#' class='button'>Previous</a></li>";
}
for ($i=1; $i<=$total_pages; $i++) {
if($page==$i){
echo "<li class='association_page active'>".$i."</li>";
} else {
echo "<li class='association_page' >".$i."</li>";
}
};
if($page < $total_pages){
echo "<li class='association_page'>NEXT</li>";
}
echo "</ul>";
Onclick i am passing the page number to php file and reponse the results json encoding. How to show
Json Output:
{"page":"8","html_content":"<li class=\"14\"></li>"}
I have not shared full content

Related

How to create pagination that shows previous and next 3 pages

I want to show a pagination which only shows the recent and next 3 pages, but I can't get it working.
Here's my current code:
$stmt = MySQL::connection3()->prepare("SELECT COUNT(ID) AS TOTAL FROM products");
$stmt->execute();
$row = $stmt->fetch();
$total_pages = ceil(intval($row["TOTAL"]) / $results_per_page);
foreach (range(1, $total_pages) as $i) {
if ($i == $page) {
?>
<li class="page-item active">
<?php echo $i ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i ?>
</li>
<?php
}
}
Thanks!
I create this example you can run and adapt with your code:
<style>.active{color:green!important}</style>
<?php
echo get_pagination_links('10','100');
function get_pagination_links($current_page, $total_pages)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$i = max(2, $current_page - 3);
for (; $i < min($current_page + 4, $total_pages); $i++) {
if($i==$current_page){
$links .= "<li class='page-item active'><a href='?page=$i;' class='page-link'>$i</a></li>";
}else{
$links .= "<li class='page-item'><a href='?page=$i;' class='page-link'>$i</a></li>";
}
}
return $links;
}
}
?>
Output:
7-8-9-10-11-12-13
obviously I didn't connect a database and therefore I used a static result

pagination in php with 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..
<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..

how to set first page pagination link to active by default

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>";
}
}

Error displaying the active state of bootstrap pagination

I am using Bootstrap to display pagination on my pages but I have an issue. I want to use the bootstrap active class to show the current page but I don't know how to go about it.
When I add the active class (as shown in the code below), all the links become active. Please what do I do?
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 6;
$sql = "SELECT * FROM offers ORDER BY i.id ASC LIMIT $start_from, 6";
$sql2 = "SELECT COUNT(id) FROM offers";
$rs_result = $db-> query($sql2, array('id' => $_SESSION['id']));
$row = $rs_result->fetch();
$total_records = $row[0];
echo $total_records;
$total_pages = ceil($total_records / 6);
for ($i=1; $i<=$total_pages; $i++) {
;echo"
<ul class='pagination'>
<li class='active' ><a href='myoffer.php?page=$i' >$i</a></li>
</ul>
";
};
?>
You should add a condition that tells which page is currently being displayed (based on the value of the query string 'page', also you should put the echo <ul line before your loop:
echo "<ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
if ($page == $i)
echo "<li class='active' >";
else
echo "<li>";
echo "<a href='myoffer.php?page=$i' >$i</a></li>";
};
echo "</ul>";
This is a copy of Aram Tchekrekjian's answer
($page == $i) ? "<li class='active' >" : "<li>";
Even better than that is to have the ternary operator inside of the <li>
tag and task it with echo class ="active" portion. So it would look like this
<li <?php ($page == $i) ? class="active" : false;?>>1</li>

help with PHP pagination

hello guys im using this second part of pagination script to show pagination;
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
echo " <li class='active'>$pageno</li>";
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
like you see here: echo " <li class='active'>$pageno</li>"; i only can show first, prev, active page, next, last page..
my question is: i want to show more pages near active page.. how can i do this?
i mean pagination style is now like:
FIRST PREV 1 NEXT LAST
i want
FIRST PREV 1 2 3 4 5 6 7 NEXT LAST
thanks
Just add a loop which loops from the first to the last page numbers:
for($page_number = 1; $page_number <= $amount_of_pages; $page_number++)
if($page_number == $pageno)
echo " <li class='active'>$page_number (active)</li>";
else
echo " <li class='active'>$page_number</li>";
To let this work you'd need to find the maximum amount of items and divide it with the amount of items on a page:
$result = mysql_query("SELECT COUNT(*) FROM table");
$row = mysql_fetch_row($result);
$amount_of_items = $row[0];
$amount_of_pages = $amount_of_items / 10; // 10 items on a page
Of course you need to add some checks and stuff, this only shows the basic principles.
Between the part which displays the previous page links and the part which displays the next page links, you need a loop between 1 and total pages.
<?php
$currentPage = 3;
$totalPages = 10;
?>
First page
Prev page
<?php
for ($i = 0; < $totalPages; $i++) {
printf('Page %d', $_SERVER['PHP_SELF'], $i, ($i == $currentPage ? 'active' : ''), $i);
}
?>
Next page
Last page
<?
if ($pageno == 1) {
echo "<li class='previous-off'>«« İlk Sayfa</li> <li class='previous-off'>« Önceki Sayfa</li> ";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=1'>«« İlk Sayfa</a></li> ";
$prevpage = $pageno-1;
echo "<li> <a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$prevpage'>« Önceki Sayfa</a> </li>";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<li class='active'>$pageno</li>";
}
else {
echo "<li><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$page_number'>$page_number</a></li>";
}
if ($pageno == $lastpage) {
echo " <li class='previous-off'>«« Sonraki</li> <li class='previous-off'>« Son Sayfa</li> ";
} else {
$nextpage = $pageno+1;
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$nextpage'>Sonraki »</a></li> ";
echo " <li class='next'><a href='{$_SERVER['PHP_SELF']}?isim=$kid&sayfa=$lastpage'>Son Sayfa »»</a></li> ";
} // if
?>
this is fixed version. works fine.

Categories