Php pagination display 5pages range

Asked
Active3 hr before
Viewed126 times

8 Answers

paginationdisplay
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Making statements based on opinion; back them up with references or personal experience., The only page numbers displaying is 5 and 10 – Amit Creation Jul 17 at 18:43 , Is there any downside to paying off a mortage shortly before moving?

// loop to show links to range of pages around current page
for ($x = 1;  $x <= ($totalpages); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
       //limit for each 5 pages
      if ($x % 5 == 0) { 
      // if we're on current page...
         if ($x == $currentpage) {
            // 'highlight' it but don't make a link
            echo " [<b>$x</b>] ";
           // if not current page...
         } else {
            // make it a link
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
         } // end else
      }
   } // end if 
} // end for
88%

The solution, ubiquitous these days, is to split your data over a number of pages while displaying navigation links to let people navigate between pages.,We can display a single page - the currently selected page - of data by making use of the LimitIterator class:,I got a little problem. I such this Pagination on an wordpress page. On startpage it works fine, but on other pages the link for every page looks like ?page=x but it links to /x,Here we've created a dataset of just the numbers 1 through 150, and broken them up into blocks of 20 per page using just the code shown in the next section:

load more v
72%

function pageRange(page, pageCount) {

   var start = page - 2,
      end = page + 2;

   if (end > pageCount) {
      start -= (end - pageCount);
      end = pageCount;
   }
   if (start <= 0) {
      end += ((start - 1) * (-1));
      start = 1;
   }

   end = end > pageCount ? pageCount : end;

   return {
      start: start,
      end: end
   };
}
65%

$first_product_displayed : This will be the number of the first product in the range we’re showing when we’re paginate. It’s the next product after the others have been offset. We’ll use this to show the user which range of products they’re viewing.,$range : This variable will use the values of $first_product_displayed, $last_product_displayed and $total_products to let the user know where they are in the range of products.,$filtered_category_query : Checks if there’s a filter active. If there is, it will save the query.,$filtered_products : When the category filter is active, we’ll store products that match the query into this array.

Here’s the HTML:

<!DOCTYPE html>
<html>
<head>
	<title>Pagination Demo</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

	<style>

		body {
			background-color: #eee;
			min-height: 100vh;
		    display: flex;
		    align-items: center;
		}

		.container {
			background-color: #fff;
			border: 1px solid #ddd;
			border-radius: 10px;
			max-width: 800px;
			padding: 20px;
		}

		.filter {
			background-color: #eee;
			padding: 10px 20px;
			margin: 0 0 10px;
			text-align: center;
			border-radius: 5px;
			vertical-align: middle;
		}

		form {
			width: 100%;
			display: flex;
    		align-items: center;
    		justify-content: center;
		}

		label, input, select {
			display: inline-block !important;
			margin-right: 10px;
			width: auto !important;
			margin-bottom: 0; 
		}

		.product {
			padding: 20px;
		}

		.column-inner {
			border: 1px solid #ddd;
		}

		.img-fluid {
			width: 100%;
			height: auto;

		}

		.pagination {
			border-top: 1px solid #ddd;
			padding-top: 10px;
			margin-top: 20px;
			width: 100%;
			text-align: center;
			justify-content: center;
		}

		.product-title, .product-price,.product-cat {
			text-align: center;
		}

		.product-title {
			padding-top: 10px;
			font-size: 24px;
			color: #454545;

		}

		.product-price {
			font-size: 20px;
			color: #0062cc;
		}

		.product-cat {
			font-size: 16px;
			color: #767676;
		}

	</style>
</head>
<body>
	<div class="container">
		<div class="filter row">
			<form class="form" action="" method="GET">
				<label>Filter Products</label>
				<select name="category" class="form-control form-control-sm">
					<option value="" ></option>

					<!-- Category drop down filter will go here -->

				</select>
				<input type="submit" class="btn btn-primary btn-sm" />

			</form>

		</div> <!-- Filter -->

		<div class="products row">

			<!-- Products will go here -->

		</div><!-- Products -->

		<nav aria-label="Page navigation">
		 	<ul class="pagination">
		 		<!-- Pagination list items will go here -->
		  	</ul>
		</nav>

	</div> <!-- Container -->
</body>
</html>
load more v
75%

For someone who had little database and programming knowledge, seeing those table rows show up onscreen based on the code I wrote (okay, so I copied an example from a book) gave me a triumphant feeling. I may not have fully understood all the magic at work back then, but that first success spurred me on to bigger and better projects.,I can remember years ago, when I first began coding in PHP and MySQL, how excited I was the first time I got information from a database to show up in a web browser.,This example will be composed of two scripts: the reusable paginator class and the index file that will display the table items and controls.,While my level of exuberance over databases may not be the same as it once was, ever since my first 'hello world' encounter with PHP and MySQL I've been hooked on the power of making things simple and easy to use.

The paginator class will have only two methods and the constructor. We will build it gradually, explaining each step as we move forward.

< ? php

class Paginator {

   private $_conn;
   private $_limit;
   private $_page;
   private $_query;
   private $_total;

}
load more v
40%

Sample preview (customizable),Download your desired php pagination class from branch (php 8 / php 7),Display pagination links where you want., Customization

Download and include Pagination class.

require("Pagination.php");

$pg = new Pagination($con, $quey, 'bind_function');
load more v
22%

Код учебника создает ссылки на страницы, как показано ниже.

For page 1: [1] 2 3 4 > >>
   For page 6: << < 3 4 5[6] 7 8 9 > >> (3 pages range before and after).

Я хочу изменить только 5 страниц.

For page 1: [1] 2 3 4 5 > >>
   For page 3: 1 2[3] 4 5 > >>
   For page 5: 1 2 3 4[5] > >>
   For page 6: << < [6] 7 8 9 10 > >>
   For page 10: << < 6 7 8 9[10] > >>

Я думаю, что эту часть нужно изменить. Я пытался искать другие статьи, но я не мог найти их. что такое хорошая логика для изменения? Спасибо.

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

Последняя страница также в формате 5 страниц

For page 1: [1] 2 3 4 5 > >>
   For page 3: 1 2[3] 4 5 > >>
   For page 5: 1 2 3 4[5] > >>
   For page 6: << < [6] 7 8 9 10 > >>
   For page 10: << < 6 7 8 9[10] > >>
   For last page 12: << < 8 9 10 11[12]

Как-то я заставляю его работать... Но текущая страница всегда находится в центре, который мне не нужен..: (

// range of num links to show
//$range = 3;
$range = 2;

....

// Added from here...    
if (($currentpage - $range) <= 1){
    $start_x = 1;
    $end_x = 5; 
}
else if ($currentpage >= ($totalpages - $range)){
    $start_x = $totalpages - 4;
    $end_x = $totalpages;
}
else {  
    $start_x = $currentpage - $range;
    $end_x = $currentpage + $range;
}
// Until here

// loop to show links to range of pages around current page
// for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
for ($x = $start_x; $x < ($end_x + 1); $x++) {
   // if it a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

Результат:

For page 1: [1] 2 3 4 5 > >>
   For page 3: 1 2[3] 4 5 > >>
   For page 5: << < 3 4[5] 6 7 > >>
   For page 6: << < 4 5[6] 7 8 > >>
   For page 10: << < 8 9[10] 11 12 > >>
   For last page 12: << < 8 9 10 11[12]

Я все еще хочу как..

For page 1: [1] 2 3 4 5 > >>
   For page 3: 1 2[3] 4 5 > >>
   For page 5: 1 2 3 4[5] > >>
   For page 6: << < [6] 7 8 9 10 > >>
   For page 10: << < 6 7 8 9[10] > >>
   For last page 12: << < 8 9 10 11[12]
load more v
60%

The forge method allows you to create a new pagination instance, and configure it by passing an array., Let's assume you have a total of 20 pages you can display. You have set num_links to 5, displaying links for 5 pages in total, and you're displaying previous and next links as well. The page you have currently selected is page 6. ,The pagination class allows you to easily setup pagination for records you display.,The get method allows you to get a configuration item on the default instance.

How To Paginate with the Query Builder

$config = array(
   'pagination_url' => 'http://localhost/fuel/welcome/index/',
   'total_items' => 10,
   'per_page' => 5,
   'uri_segment' => 3,
   // or if you prefer pagination by query string
   //'uri_segment'    => 'page',
);

// Create a pagination instance named 'mypagination'
$pagination = Pagination::forge('mypagination', $config);

$data['example_data'] = DB::select('id', 'value') -
   > from('pagination') -
   > limit($pagination - > per_page) -
   > offset($pagination - > offset) -
   > execute() -
   > as_array();

// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;

// return the view
return\ View::forge('welcome/index', $data);

How To Paginate with ORM

$config = array(
   'pagination_url' => 'http://localhost/fuel/posts/index/',
   'total_items' => Model_Post::count(),
   'per_page' => 10,
   'uri_segment' => 3,
   // or if you prefer pagination by query string
   //'uri_segment'    => 'page',
);

$pagination = Pagination::forge('mypagination', $config);

$data['example_data'] = Model_Post::query() -
   > rows_offset($pagination - > offset) -
   > rows_limit($pagination - > per_page) -
   > get();

// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;

// return the view
return\ View::forge('posts/index', $data);
load more v

Other "pagination-display" queries related to "Php pagination display 5pages range"