PHP pagination with page limit - php

I want to limit the pages out of the total number of pages.
Example: 1 2 3 4 5 6 >> out of 30
Example2: << 5 6 7 8 9 10 >> out of 30
Here is my code:
$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
// records per page
$per_page = 5;
// total records in database
$total_count = Mp3_Model::count_all();
// instantating the $pagination
$pagination = new Pagination($page, $per_page, $total_count);
// find the records for this page
$sql = "SELECT * FROM mp3 ";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$mp3s = Mp3_Model::find_by_sql($sql);
foreach ($mp3s as $mp3) {
echo $mp3->titlu;
}
and this is the pagination:
<?php
if ($pagination->total_pages() > 1) {
if ($pagination->has_previous_page()) {
echo '<li>«</li>';
}
for ($i = 1; $i <= $pagination->total_pages(); $i++) {
echo '<li';
if ($_GET['page'] == $i) {
echo ' class="active"';
}
echo '>' . $i . '</li>';
}
if ($pagination->has_next_page()) {
echo '<li>» </li>';
}
}
?>

You can use jQuery pagination, it is a much better solution.

Related

How to limit pages in pagination in php

i want to make the pages not go beyond 10
this the code that shows the number of pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}`
this is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
and i want to make it go like this
1 2 3 4 5 6 7 8 9 10 ...
and if im on page 10 it will show like this
1 .. 10 11 12 13 14 15 16 17 18 19 20 ... 25
i don't have any idea how to make this work
this is the source
<?php
if(isset($_POST['search']))
{
unset($_SESSION['keyword']);
$KeyWord = $_POST['keyword'];
$_SESSION['keyword'] = $KeyWord;
}
$KW = $_SESSION['keyword'];
$results_per_page = 1;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author
LIKE '%$KW%' ORDER BY Book_Title";
$result = mysqli_query($connect, $sql);
$number_of_results = mysqli_num_rows($result);
$number_of_pages = ceil($number_of_results/$results_per_page);
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*$results_per_page;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author LIKE '%$KW%' ORDER BY Book_Title LIMIT $this_page_first_result, $results_per_page";
$result = mysqli_query($connect, $sql);
$query = "SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author LIKE '%$KW%' ORDER BY Book_Title ASC";
$sql = mysqli_query($connect, $query);
$nurows = mysqli_num_rows($sql);
$numrows = $connect->query($query);
if($nurows == 0 OR $nurows == 1)
{
$restext = "result";
}
else
{
$restext = "results";
}
if(isset($_POST['search']))
{
$num = 0;
$desc = "result. Keyword is blank... Please try again.";
}
else
{
$num = '';
$desc = '';
}
?>
<p><strong><?php $num ?> </strong><?php $desc ?> </p>
<?php
?> <p><strong><?php echo $nurows; ?> </strong> <?php echo $restext;?> for '<?php echo $KW; ?>'</p>
<?php
while($row = mysqli_fetch_array($result)) {
$bookid = $row['Book_ID'];
$booktitle = $row['Book_Title'];
$bookauthor = $row['Book_Author'];
$bookimg = $row['Book_IMG_Name'];
echo "<hr><img src='../../images/bookimg/$bookimg' class='bookimg'><h3>$booktitle</hr><p><font style='font-size: 15px;'><font style='font-style: italic;'>by " . $bookauthor . '</p></font><br />';
}
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
try the following:
function pageList($pages, $current_page)
{
$output = '';
$previous_page = 0;
foreach($pages as $page)
{
if ($page - $previous_page > 2)
{
$output.= '... ';
}
else
if ($page - $previous_page === 2)
{
$output.= '... ';
}
$output.= "$page ";
$previous_page = $page;
}
return $output;
}
function createPagination($current_page, $number_of_pages)
{
$page_span = 5;
$middle_min = max($current_page - $page_span, 1);
$middle_max = min($current_page + $page_span, $number_of_pages);
$pages = range($middle_min, $middle_max);
if ($middle_min !== 1)
{
array_unshift($pages, 1);
}
if ($middle_max !== $number_of_pages)
{
$pages[] = $number_of_pages;
}
return pageList($pages, $current_page);
}
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$number_of_pages = 25;
echo createPagination($current_page, $number_of_pages);
This will dispaly the 5 pages before and after of the selected page. Just edit the $page_span to determine how many of the pages you want to show. This uses a simple loop to loop over range of values from the minimum to maximum range.

Pagination Prev - Next Increment

i create a php news system, but i have a problem:
<?php
include('config.php');
if( isset( $_GET["page"]) ) $PAGE=$_GET["page"]; else $PAGE=1;
$query1=mysql_query("select id, name, email , age from addd LIMIT ". (($PAGE * 5) - 5) .",5");
echo "<table><tr><td>Testo</td><td>Nome</td><td>Anni</td></tr>";
function truncate_string($str, $length) {
if (!(strlen($query2['name']) <= $length)) {
$query2['name'] = substr($query2['name'], 0, strpos($query2['name'], ' ', $length)) . '...';
}
return $query2['name'];
}
while($query2=mysql_fetch_array($query1))
{
$number= $query2['name'];
echo "<tr><td>".substr($query2['name'], 0, 500)."...</td>";
echo "<td>".$query2['email']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td>".str_word_count($number)."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Mod</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."' onclick=\"return confirm('Sei sicuro di volerlo eliminare?');\");'>Canc</a></td><tr>";
echo "<td><a href='singletwo.php?id=".$query2['id']."');'>vedi</a></td<tr>";
}
?>
The pages follow this numbering: ?page=1, ?page=2 ecc.
Each page contains 5 news.
How do I create an automatic pagination system?
With Prev-Next, automatically detect possible next or previous pages?
I don't know how to do it.
Start by having the max length and total number of rows in variables:
<?php
include('config.php');
$max = 5;
$total = mysql_query("select count(*) from addd");
$PAGE = isset($_GET["page"]) ? $_GET["page"] : 1;
$query1 = mysql_query("select id, name, email , age from addd LIMIT " . (($PAGE * $max) - $max) . "," . $max);
That way, you can calculate how many pages you'll need.
The following code will give you a page list (Page 1, Page 2, Page 3 etc.):
for($i = 0; $i < ceil($total / $max); $i ++)
{
$p = $i + 1;
echo 'Page ' . $p . '';
}
If you'd rather have Previous and Next links, try this:
if($PAGE > 1)
echo '<a href="?page=' . ($PAGE - 1) . '>Previous</a>';
if(ceil($total / $max) > $PAGE)
echo '<a href="?page=' . ($PAGE + 1) . '>Next</a>';
What you could do is:
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = $currentPage*5;
$offset = $offset-5;
Now that you have these numbers, you can use them in your query:
$stmt = "SELECT
...
FROM news
LIMIT ".$offset.", ".$limit.";
This way you'll get the records you want. As far as the next and previous buttons go:
if ($currentPage > 1) {
// Show previous button
}
For the next button you'll need to do another query:
$stmt = "SELECT COUNT(*) as total FROM news";
$result = $pdo->fetch();
$totalRows = $result['total'];
if ($currentPage < round($totalRows/5)) {
// Show next button
}

Pagination Number of Pages

I am creating pagination. I have a pagination class:
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=10, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset(){
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages(){
return ceil($this->total_count/$this->per_page);
}
public function previous_page(){
return $this->current_page - 1;
}
public function next_page(){
return $this->current_page + 1;
}
public function has_previous_page(){
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page(){
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
I also am using it like this:
<?php
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page
$per_page = 8;
// 3. total record count ($total_count)
$total_count = Song::count_all();
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
// for this page
$sql = "SELECT * FROM songs ";
$sql .= "ORDER BY dopeness DESC ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$songs = Song::find_by_sql($sql);
?>
<?php
if($pagination->total_pages() > 1){
if($pagination->has_previous_page()){
echo "<a href=\"all_songs.php?page=";
echo $pagination->previous_page();
echo "\">«Previous </a> ";
}
for($i=1; $i<=$pagination->total_pages(); $i++){
if($i == $page){
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " {$i} ";
}
}
if($pagination->has_next_page()){
echo "<a href=\"all_songs.php?page=";
echo $pagination->next_page();
echo "\"> Next»</a> ";
}
}
?>
Output is "previous 1 2 3 (however many pages) next".
What I want to do is make it so that if there are more than ten pages there will be "..." for the last link that would take you to page 11-20 ("previous 11 12 13 next") and so on. Can anybody help me to tackle this?
You should not only display the next 10 pages starting with 0, but consider the current page - then walk 10 pages on wards, and break the iteration, once you displayed 10 page-buttons:
$page = 5; //assuming within valid bounds
for($i=$page; $i<=$pagination->total_pages(); $i++){
if ($i > $page+ 10){
echo " ... ";
break;
}
if($i == $page){
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " {$i} ";
}
}

PHP Pagination with 5 pages

if( isset($_GET['page'] ) )
{
$page = $_GET['page']-1;
$offset = $recLimit * $page;$page = $_GET['page']+1;
}
else
{
$page=2;
$offset = 0;
}
$phpself = $_SERVER['PHP_SELF'];
if( $totalPages <= $noofpages )
{
echo "Pages if less than or equal 5 : ";
for($i=1; $i <= $totalPages; $i++)
{
echo "$i |";
}
}
else
{
$i=1;
for($i; $i <= $totalPages; $i++)
{
echo "$i |";
}
}
?><table border="1" cellpadding="8"><tr> <th>ID</th><th>Name</th><th>Age</th><th>E-mail ID</th><th>Verified</th>
I'm goin to make a PHP pagination but i've some problem, in this i want to show 5 pages and 4 records per page. and if we click on next button then it shows the remaining page. And there are total 7 pages.
Since I couldn't really understand your code, here is something to get you started:
$items_per_page = 4;
/* Get the total number of records */
$result = mysqli_query("select count(*) as n from ...");
$row = mysqli_fetch_assoc($result);
$total_count = $row["n"];
/* Calculate the total number of pages */
$total_pages = ceil($total_count / $items_per_page);
/* Determine the current page */
$current_page = intval(#$_GET["page"]);
if(!$current_page || $current_page < 1) { $current_page = 1; }
if($current_page > $total_pages) { $current_page = $total_pages; }
/* Get the records for the current page */
$limit = $items_per_page;
$offset = ($current_page - 1) * $items_per_page;
$result = mysqli_query("select ... limit " . $offset . ", " . $limit);
To display the pagination links, use this:
for($i = 1; $i <= $total_pages; $i++)
{
if($i > 1) { print(" | "); }
if($current_page == $i) { print($i); }
else { print('' . $i . ''; }
}

PHP : Custom pagination in Wordpress

I use this code for my custom paging :
global $wpdb, $table_prefix, $current_user;
get_currentuserinfo();
$umail = $current_user->user_email;
$paged = $wpdb->get_results("SELECT * FROM {$table_prefix}comments WHERE comment_author_email = '$umail'");
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = COUNT($paged);
$pages = ceil($pages / 2);
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $querystring .= "$key=$value&";
}
// Pagination
for ($i = 1; $i <= $pages; $i++) {
echo "<a " . ($i == $page ? "class=\"selected\" " : "");
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
This code paginate my comments look like this : 1 2 3 4 5 6 7 8 9 10 11
How can change code to get paginate look like this: 1 2 3 ... 11
Thanks for any help.
Try the following loop:
$page = 8;//current page
$pages = 15;//count of all pages
$batch_middle = 5;//the approximate number of pages in the middle links to display. Current page will be in the middle
$batch_lr = 1;//number of pages in the left and right links
for($i = 1; $i <= $pages;$i++)
{
//display first links
if ($i <= $batch_lr)
{
echo $i . ' ';
continue;
}
//display last links
if ($i > $pages-$batch_lr)
{
echo $i . ' ';
continue;
}
//display middle links
if ($i>=($page-floor($batch_middle/2)) && $i<=($page+floor($batch_middle/2)))
{
echo $i . ' ';
continue;
}
//placeholder
echo ' ... ';
//move the pointer
$i = ($i < $page) ? ($page-floor($batch_middle/2)-1) : ($pages-$batch_lr) ;
}
//output example 1: 1 2 ... 14 15
//output example 2: 1 2 ... 7 8 9 ... 14 15
//output example 3: 1 2 3 4 5 ... 14 15
//output example 4: 1 ... 6 7 8 9 10 ... 15
Replace $page and $pages with your logic with getting a current page and count of total pages.
Replace the $batch_middle and $batch_lr to configure a number of links in the first, second and third batches of the links

Categories