MySQL/PHP - page number pagination Only show 10 pages at times - php

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>

function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}

try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}

You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}

Related

php pagination on second page not working

$dwdb=mysqli_connect("localhost","root","","dw");
//this is my page number
$page=(isset($_GET['page']) && $_GET['page']>0)?$_GET['page']:1 ;
//this is my cat_id
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
$perpage=2;
$limit=($page > 1)?($page*$perpage)-$perpage:0;
$query=mysqli_query($dwdb,"select *from movies where y_id='$new' limit {$limit},{$perpage}");
while($result=mysqli_fetch_array($query)){
$id=$result['m_id'];
$name=$result['title'];
$img=$result['image'];
echo"<div><a href='downloadpage.php?yc=$id'>$id.....$name<br><img src='i/image/$img' style='height:200px;width:200px;'/></a></div>";
}
$query1=mysqli_query($dwdb,"select *from movies where y_id='$new'");
$total=mysqli_num_rows($query1);
$pages=ceil($total/$perpage);
echo "<a href='index1.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index1.php?page=$pages'>Last page</a>";
That is my code. The problem is that on my second page i have result of first $new variable .......................................................................................................................................................................
... i have a problem that on my second page i have result of first $new variable
That's because you're not including $new variable in the pagination links. So every time you go to 2nd, 3rd, 4th, ... page, you'll get the same $new value as 1, and that's because of this statement,
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
Include this variable in the pagination links so that you could get it's value in the subsequent pages. So your pagination links section would be like this:
// your code
echo "<a href='index1.php?page=1&year=".$new."'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."&year=".$new."'>".$i."</a> ";
}
echo "<a href='index1.php?page=".$pages."&year=".$new."'>Last page</a>";
for pagination use this code in file with name: view-paginated.php
$per_page = 10;
$result = mysql_query("SELECT * FROM table");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
echo "<p><a href='view.php'>show all</a> | <b>page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
echo mysql_result($result, $i, 'YOUR COLUMN') ;
}

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

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 Pagination with x amount of pages

Currently I have a functioning pagination script, although I'm missing on feature. At the moment it's possible for hundreds of pages to be showing in the $pages list, because there's no filter on to show between, for example, [1] [...] 5, 6, 7 [..] [45]. Here's my code:
/** Pagination **/
$limit = 7;
$query = "SELECT COUNT(*) FROM users";
$result = $db->prepare($query);
$result->execute();
$pages_query = $result->fetchColumn(0);
$count = number_format($pages_query);
$pages = ceil($pages_query / $limit);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;
ob_start();
echo("<span class='alignright'>");
if ($pages >= 1 && $page <= $pages){
if($page > 1){
$next = ($page - 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-left'></i></a>");
}
for ($x=1; $x<=$pages; $x++){
echo ($x == $page) ? "<strong style='font-weight: bold!important;'><a class='paginate' href='?page=$x'>$x</a></strong>" : "<a class='paginate' href='?page=$x'>$x</a>";
}
if($page < $pages){
$next = ($page + 1);
$link = "?page=$next";
echo("<a class='paginate' href='$link'><i class='icon-caret-right'></i></a>");
}
echo("</span>");
if($count > 0){
echo("<span class='smalltext'>Page <strong class='half'>$page</strong> of $pages:</span>");
} else {
echo("<span class='smalltext'>There are <span class='half'>$count</span> results to display.</span>");
}
$pagintion = ob_get_clean();
(It's been stripped from other junk that was in it but that's the general frame.) Basically I'm trying to figure out how to limit it to have "between pages" down the bottom as specified in the top part of the question. Something like:
[<] [1] ... [4] [5] [6] ... [45] [>]
If that makes sense.
you can try LIMIT with in your query itself
SELECT * FROM TABLE_NAME LIMIT STARTING_RESULT_NUMBER, RESULTS_PER_PAGE
RESULTS_PER_PAGE is no.of items per page you want to display
STARTING_RESULT_NUMBER =(CURRENT_PAGE_NUMBER*RESULTS_PER_PAGE)
In order to change the list of pages to something like [<] [1] ... [4] [5] [6] ... [45] [>] rather than showing all pages numbers, you can replace your for loop by something like :
echo "<a class='paginate' href='?page=1'>1</a>";
if($page > 3) {echo "...";}
if($page > 2) {echo "<a class='paginate' href='?page=" . $x-1 . "'>" . $x-1 "</a>";}
if($page != 1 && $page != pages) {echo "<a class='paginate' href='?page=" . $x . "'>" . $x "</a>";}
if($page < $pages-1) {echo "<a class='paginate' href='?page=" . $x+1 . "'>" . $x+1 "</a>";}
if($page < $pages-2) {echo "...";}
if($pages >1) {echo "<a class='paginate' href='?page=1'>1</a>";}
Il will show the first page, the last pages, the current pages and the ones just before and just after.
My personal meaning with pagination is that it must be readable and simple to change later on.
I typed the following code based on you're example:
$totalPages = 145; //the total amount of pages
$selectedPage = 40; //the selected page
$pages = array(); //the array which is gonna hold the pages we need to display
$offset = 3; //the number of pages to select around the selected page
$closePages = range($selectedPage - $offset, $selectedPage + $offset); //select the pages that are in $offset of the selected page
array_filter($closePages, function($x) { //filter the pages below 1 and above $totalPages
return ($x <= $totalPages && $x >= 1 ? true : false );
});
array_push($pages, 1); //add the first page
array_push($pages, '...'); //add some dots
$pages = array_merge($pages, $closePages);
array_push($pages, '...'); //and again add some dots
array_push($pages, $totalPages); //add the last page
Then you use a foreach loop to display the pages:
foreach($pages as $page) {
if (is_numeric($page)) {
if ($page != $selectedPage) $content .= ' ' . $page . ' ';
else $content .= ' <strong>' . $page . '</strong> ';
} else
$content .= '[...]';
}
Some explanation after you're comment on this answer:
The $totalPages variable must be the total amount of pages on the page (from you're example) and the $selectedPage is the page that is selected at this moment.
$totalPages = ceil($pages_query / $limit);
$selectedPage = isset($_GET['page']) ? $_GET['page'] : 1;

PHP & MySQL pagination display problem

I asked a similar question like this yesterday but after waiting for ever I figured out part of the problem but now I'm stuck again I'm trying to display ... when the search results are to long because my pagination links will keep on displaying and will not stop until every link is displayed on the page.
For example I'm trying to achieve the following in the example below. Can some one help me fix my code so I can update my site. Thanks
This is what I want to be able to do.
First Previous 1 2 ... 5 6 7 8 9 10 11 12 13 ... 199 200 Next Last
Here is my pagination code that displays the links.
$display = 20;
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(id) FROM comments WHERE user_id=3";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display) {
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
//content goes here
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if ($current_page != 1) {
echo 'First';
}
if ($current_page != 1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo '<span>' . $i . '</span> ';
}
}
if ($current_page != $pages) {
echo 'Next';
}
if ($current_page != $pages) {
echo 'Last';
}
echo '</p>';
}
Instead of the loop just use something like this:
if($current_page > 8 && $pages > 11) {
echo '1 ';
echo '2 ';
echo '...';
}
for ($i = max(1, $current_page - 4); $i < min($current_page + 4, $pages); $i ++) {
echo '' . $i . ' ';
}
if ($current_page < $pages - 8 && $pages > 11) {
echo '...';
echo '' . ($pages - 1) . ' ';
echo '' . $pages . ' ';
}

Categories