The pagination here works fine, but addition to pagination, as the limit per page is 3 suppose total 8 items in database so i want to display "
showing 1 t0 3 item of 8" on page one, "showing 4 to 6 items of 8" on page two and so on. please help
$recordsLimit = 3;
$page = isset($_GET['page']) ? intval($_GET['page']): 1;
$totalProducts = countProducts($selectedCategoryId);
$totalPages = ceil($totalProducts / $recordsLimit);
$pageNumber = $recordsLimit * ($page - 1);
$products = getProductsByCatId($selectedCategoryId, $pageNumber, $recordsLimit);
<?php if($totalProducts > $recordsLimit) : ?>
<div class="pagination">
<span>Page <?php echo $page.' of '.$totalPages; ?></span>
<?php for($i=1; $i <= $totalPages; $i++) :
if($i == $page) { ?>
<strong><?php echo $i; ?></strong>
<?php } else { ?>
<?php echo $i; ?>
<?php }
endfor; ?>
<?php endif; ?>
Try:
echo "Showing ".( $page == 1 ? 1 : ($page -1) * $recordsLimit +1 )." to ".($page * $recordsLimit)." item of ".$totalProducts;
Simply use this:
<?php $firstNum = (($page-1)*$recordsLimit+1) ?>
showing <?php echo $firstNum; ?> to <?php echo $firstNum+2;?> items of <?php echo $totalProducts;?>
To fix the problem with Tom's code where the last page has an incorrect "to" number I added an offset variable. For this to work the $recordsLimit variable has to match the "showposts" argument in the query. The OP did not show his query so I'm showing the one I used.
$total_results = $wp_query->found_posts;
$recordsLimit = 10;
$args['post_type'] = 'listings';
$args['showposts'] = $recordsLimit;
$args['paged'] = $paged;
$wp_query = new WP_Query($args);
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$countoffset = ($page * $recordsLimit)-($total_results);
$countfrom = ( $page == 1 ? 1 : ($page -1) * $recordsLimit +1 );
$countto = ($page * $recordsLimit);
if(($total_results - $countfrom) < $recordsLimit) {$countto = ($countto - $countoffset);}
echo 'Showing '.$countfrom.' to '.$countto.' of '.$total_results.' total results';
Related
I am sorry if the question is very basic or someone might post this question before but i can't figure out this code. Any help appreciated
Pagination works good but it shows 2 more blank pages even there are no posts on that page.
php:
<?php
$page = (isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$perpage = 10;
//$limit = ($page > 1) ? (((($page *2)+1)-3)* $perpage) - $perpage : 0;
$limit = ((($page*2)+1)-3)*$perpage;
$query = mysqli_query($dbc, "SELECT SQL_CALC_FOUND_ROWS * FROM test WHERE Category='Pharma' LIMIT {$limit}, {$perpage}");
$records = mysqli_fetch_all($query);
$total = mysqli_query($dbc, "SELECT FOUND_ROWS() as total");
$total = mysqli_fetch_assoc($total)['total'];
$pages = ceil($total/$perpage);
?>
//prev
<?php
if($page>1){
?>
<a class="page-link" href="?page=<?php $pagep = $page -1; echo $pagep; ?>" tabindex="-1">Previous</a>
<?php
}
?>
//next
<?php
if($page<$pages){
?>
<a class="page-link" href="?page=<?php $pagen = $page +1; echo $pagen; ?>">Next</a>
<?php
}
?>
I tried to change the query as i know i fetch $total up here not the $query but when i fetch $query pagination not working. Thanks in advance.
I am having difficulties understanding how to limit the number of pages with pagination. I am trying to list my articles in my DB but I have over 500 pages! I would only like to show a max of about 15 page numbers at a time. I can't seem to understand the answers given in the other questions.
Here is my code for the Pagination:
<?php
require 'core/init.php';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if(!isset($_GET['page'])){
$_GET['page'] = 1;
}
if(! (int)$_GET['page']){
$_GET['page'] = 1;
}
if($_GET['page'] < 1){
$_GET['page'] = 1;
}
$perPage = 18;
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$articles = DB::getInstance()->query("SELECT SQL_CALC_FOUND_ROWS * FROM articles ORDER BY added DESC LIMIT {$start}, {$perPage}");
foreach($articles->results() as $article){ ?>
<div class="article-container">
<h2><?php echo $article->title; ?></h2>
</div>
<?php
}
// pagination
$total = DB::getInstance()->query("SELECT FOUND_ROWS() as total");
$total = $total->results()[0];
foreach ($total as $total => $value) {
}
$pages = ceil($value / $perPage);
$maxpage = 15;
?>
<div class="pagination">
<?php if($_GET['page'] >= 2){
echo 'Prev';
}
for ($i=1; $i <= $pages; $i++) : ?>
<a href="?page=<?php echo $i; ?>" <?php if($page === $i) { echo 'class="pageSelected"'; } ?>><?php echo $i; ?></a>
<?php endfor;
if((int)$_GET['page'] != $i - 1 ){
echo 'Next';
}
?>
</div>
This works as expected but it only spits out the number of pages from 1 to 500. What is the best/easiest way to show something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 ... >
?
You need to change your for-cycle, the starting point and ending point
$count = 9;
$startPage = max(1, $page - $count);
$endPage = min( $pages, $page + $count);
for($i = $startPage; $i < $endPage; $i++) {
// write out the link to the page
}
then if the page is 250 it will show only 241, 242, ..., 250, ..., 257, 258
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
I have this algorithm I made for pagination.
The $array variable is a result set from the query after the page number and limit has been set. It's a result from a PDO::fetchAll() function.
The $array["totalCount"] contains the count of ALL the rows in the table, not only that set but all the rows.
What I want is to limit the number of pages displayed, and make it somehow like google style pagination, I've search a lot of similar questions online, but what makes me struggle is to guess how I can implement it in my algorithm which is much different from others.
Thanks in advance.
public static function renderPaginationBar($obj, $params) {
$array = $obj->getAll($params);
$set = $params["set"];
$perSet = $params["limit"];
$pages = ceil((int)$array["totalCount"] / (int)$perSet);
if($pages > 1 && $set <= $pages) {
$query = preg_replace('/set=\d*/i', '', http_build_query($_GET));
?>
<ul class="pagination">
<?php
if($set > 1) {
?>
<li><<< Inicio</li>
<li><<</li>
<?php
}
for($i = 1; $i <= $pages; $i++) {
$class = $set == $i ? "active" : "";
?>
<li class="<?php echo $class; ?>"><?php echo $i; ?></li>
<?php
}
if($set < $pages) {
?>
<li>>></li>
<li>Último >>></li>
<?php
}
?>
</ul>
<?php
}
unset($array["totalCount"]);
return $array;
}
you can try displaying it as a dropdown between the 'next' and 'prev' button if you want.. it would be easier to display all the page numbers..
or you can first get the value of current page, add 5 and store it as '$add' and minus 5 and store it as '$minus' then insert this in the page buttons:
<input type="submit" value="<?php echo $page_num;?>" <?php if($pagenum > $add || $pagenum < $minus){ echo "style='display:none'";?>>
it will hide the buttons that are more than 5 and less than 5 the current page
replace yor 'for' with this:
$get_add = $params["set"] + 5;
$get_minus = $params["set"] - 5;
for($i = 1; $i <= $pages; $i++) {
$class = $set == $i ? "active" : "";
?>
<li class="<?php echo $class; ?>" <?php if($i > $get_add || $i < $get_minus){ echo "style='display:none;position:absolute'";?>><?php echo $i; ?></li>
<?php
}
This might be a quite silly question, but I can't figure anything out that might help me go further. I'm looking to shorten the number of NUMBERS in my page navigation.
Instead of being like: 1, 2, 3, 4, 5, 6, 7, 8
I want it be like: 1, 2...7, 8
And when I go to 2, number 3 should now be seen in the numbers group.
Here is my code that is in charge of the page numbers:
<div id="user_nav_bottom">
<?php for($i=1; $i < sizeof($pages)+1; $i++) {
$strong = ($_GET['page'] == $i) ? ' dark bold' : '';
echo '<span class="normal' . $strong . '">' . $i . ', </span>';
} ?>
</div>
This is a modified chunk of code I wrote for paging forums.
It's output isn't exactly how you showed but should just be a matter of tweaking.
<?php
//Set up vars
$currentPage = isset($_GET["page"])? $_GET["page"] : 1;
$numPages = count($pages);
$numPages = 7;//cause I don't have the real var for testing
$howMany = 1;
?>
<?php if ($numPages > 1): ?>
<li>Page <?php echo $currentPage?> of <?php echo $numPages?></li>
<?php if ($currentPage > 1): ?>
<li>First</li>
<li><</li>
<?php endif; ?>
<?php if ($currentPage > $howMany + 1): ?>
<li>...</li>
<?php endif; ?>
<?php for ($pageIndex = $currentPage - $howMany; $pageIndex <= $currentPage + $howMany; $pageIndex++): ?>
<?php if ($pageIndex >= 1 && $pageIndex <= $numPages): ?>
<li>
<?php if ($pageIndex == $currentPage): ?>
<u>
<?php endif; ?>
<?php echo $pageIndex?>
<?php if ($pageIndex == $currentPage): ?>
</u>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endfor; ?>
<?php if ($currentPage < $numPages - $howMany): ?>
<li>...</li>
<?php endif; ?>
<?php if ($currentPage < $numPages): ?>
<li>></li>
<li>Last</li>
<?php endif; ?>
<?php endif; ?>
Check this out: https://codereview.stackexchange.com/a/10292. This solution should solve your problem. I think its a very good approach.
This one shows two links before and two after the current requested link:
<div id="user_nav_bottom">
<?php
$current = $_GET['page'];
$last = count($pages)+1;
$curr0 = $current-2;
$curr1 = $current+2;
if ($curr0<=1) {
$curr0 = 1;
$curr1 = $last>5? 5 : $last;
}
if ($curr1>=$last) {
$curr0 = $last-4 < 1 ? 1 : $last-4;
$curr1 = $last;
}
// now print all links:
echo '« ';
for ($i=$curr0; $i<=$curr1; $i++) {
$style = ($i==$current)? 'font-weight:bold':'';
echo ' '.$i.' ';
}
echo '» ';
?>
</div>
This shows links like this: « 13 14 15 16 17 »
Imho it's not so difficult to add also first five and last five links by adding appropriate conditions.
Testing script here
What you might want to do is use the "logarithmic" pagination technique described in my answer here:
How to do page navigation for many, many pages? Logarithmic page navigation
If you set LINKS_PER_STEP to a low value like 2 or 3, it'll produce very compact output.