How to create pagination that shows previous and next 3 pages - php

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

Related

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 handle the ajax pagination in 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

Pagination does not work perfectly

I have the following code:
$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1';
$init = $page - 1;
$init= $max * $init;
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";
$varstrCount = $crud->viewdatas($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_mytable"];
}
}
$result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
$varresult = $crud->viewdatas($result);
content of page:
<?php
if(count($varresult)){
foreach ($varresult as $res) {
?>
<h5><?php echo $res['title'] ?></h5>
<?php
}
}
?>
<?php
$max_links = 10;
$previous = $page - 1;
$next = $page + 1;
$pgs = ceil($total / $max);
if($pgs > 1 ){
if($previous > 0){
echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
} else{
}
for($i=$page-$max_links; $i <= $pgs-1; $i++) {
if ($i <= 0){
}else{
if($i != $page{
if($i == $pgs){ //if end insert 3 dots
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ...";
}else{
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>";
}
} else{
if($i == $pgs){ //if end insert 3 dots
echo "<li>".$i."</li> ...";
}else{
echo "<li>".$i."</li>";
}
}
}
}
if($next <= $pgs){
echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
}
}
?>
The result:
And I not understand the reason for the active number stay right off the paging menu
In the code I defined max-links for 10, but no show number 5 and if I define max links for 3 changes nothing , displays the same result as the image.
Thanks for any help
echo "<li>".$i."</li>";
on the above line add up a href, seems like you have css formatting for li>a because of which you are not getting the required formatting on only li. so for getting better formatting for all paging links current, prev, next. you need to add up a tags.
echo "<li><a>".$i."</a></li>"; //in your else part

Pagination System

I made this script for my website.
It works and everything but there is one small problem.
You can go forward infinitely
You can just click Next Page in an endless loop
And for some reason when i echo $num_pages I get 1 0_0
How can I fix the "infinie nexting" - my weird definition for the problem :)
<?php
$per_page = 4;
$start = 1;
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
if($page <= 1) {
$start = 1;
$page = 1;
} else {
$start = $page * $per_page - $per_page;
}
$next = $page+1;
$previous = $page-1;
$GetAllComments = $con->query("SELECT * FROM comments LIMIT $start, $per_page");
$num_rows = $GetAllComments->num_rows;
$num_pages = $num_rows / $per_page;
while($GAC = $GetAllComments->fetch_object()) {
echo "<div class='well'> <h3>". $GAC->Title. "</h3>
". $GAC->Content. " <hr /> <em> Posted By ". $GAC->PosterName ." </em>
</div>";
}
$pagen = $page+1;
$pagep = $page-1;
echo "
<div class='pagination'>
<ul>
";
if($page > 1) {
echo "
<li><a href='?page=$previous'>".$pagep."</a> </li>
";
}
echo "
<li class='disabled'><a href='#'>$page</a></li>
";
echo "
<li> <a href='?page=$next'>" . $pagen . "</a></li>
</ul>
</div>
";
I would put the display in a for loop:
for($i = 1; $i <= $num_pages; $i++)
{
enter code here
}
That way it will repeat for as many pages are in num_pages and not anymore.

php pagination, something wrong

Some code from me.
$files = glob("pardod/*.html");
$record_count = 5;
$total_pages = ceil(count($files)/$record_count);
$page = $_GET['page'];
$offset = ($page-1)*$record_count;
$files_filter = array_slice($files, $offset,$record_count);
for ($i = 0; $i<$filecount; $i++){
if ($page){
$start = ($page - 1) * $record_count;
}else{
$start = 0;
}
}
if($total_pages > 1){
if($page != 1){
echo 'Atpakal';
}
if($page != $total_pages){
echo 'Uz priekšu';
}
}
The php pagination dont work, i am just learning how to make, where is a problem?
The *.html files didn't shows :(
Try this code for pagination
<?php
$con=mysql_connect("localhost","root","");
$page=$_REQUEST['page'];
if ($page < 1)
{
$page = 1;
}
$resultsPerPage =15;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM tablename'));
$totalPages = ceil($numberOfRows / $resultsPerPage);
echo"<center><table border='1' bordercolor='blue' height='90%' width='90%'> <tr><th bgcolor='silver'>Name</th><th bgcolor='silver'>Password</th><th bgcolor='silver'>Question</th><th bgcolor='silver'> Answer</th><th bgcolor='silver'>Image</th> </tr>";
$i=1;
$result= mysql_query("SELECT * FROM password LIMIT $startResults, $resultsPerPage");
while($row=mysql_fetch_array($result))
{
}
echo"</tr></table></center>";
echo '<center>First&nbsp';
if($page > 1)
echo 'Back&nbsp';
for($i = 1; $i <= $totalPages; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong>&nbsp';
else
echo ''.$i.'&nbsp';
}
if ($page < $totalPages)
echo 'Next ';
echo 'Last</center>';
?>
Try This Code
$limit = ( isset($_GET['limit'])) ? $_GET['limit'] : 5;
if (strtolower($limit) == 'all') {
$limit = 'all';
} else {
$limit = filter_var($limit, FILTER_SANITIZE_NUMBER_INT);
if (trim($limit) == '') {
$limit = 5;
}
}
$page = ( isset($_GET['page'])) ? $_GET['page'] : 1;
$page = filter_var($page, FILTER_SANITIZE_NUMBER_INT);
$links = ( isset($_GET['links'])) ? $_GET['links'] : 1;
$links = filter_var($links, FILTER_SANITIZE_NUMBER_INT);
Here is the link where step by step described all the process in details.
Simple Pagination in PHP By Learning Ocean Team
if you are using bootstrap, you can use the following
function pagination($page, $count) {
global $options;
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = trim(str_replace("page=".$page, "", $actual_link), "&");
$lenght = ceil($count/$options->get("result_per_page"));
if ($page <= 3) {
if ($lenght < 5) {
$first = 0;
$last = $lenght-1;
} else {
$first = 0;
$last = 4;
}
} else if ($page < ($lenght-2)) {
$first = $page-2;
$last = $page+2;
} else if ($page <= $lenght) {
$first = $page-5;
$last = $lenght-1;
}
if ($page > 0) {
$prev = $page-1;
$next = $page+1;
} else {
$prev = 0;
$next = $page+1;
}
echo '<span>Viewing page '.($page+1).' of '.$lenght.'</span><br>';
echo '<nav aria-label="Page navigation example">';
echo '<ul class="pagination justify-content-center">';
if ($prev > 0) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page=0">««</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$prev.'">«</a></li>';
}
for ($i = $first; $i<=$last; $i++) {
if ($i == $page) {
$active = ' active';
} else {
$active = '';
}
echo '<li class="page-item'.$active.'"><a class="page-link" href="'.$actual_link.'&page='.$i.'">'.($i+1).'</a></li>';
}
if ($next < $last) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$next.'">»</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.($lenght-1).'">»»</a></li>';
}
echo '</ul>';
echo '</nav>';
}
to call the function
$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);

Categories