这里的分页工作正常,但除了分页,因为每页的限制为3,假设数据库中共有8个项目,所以我想显示“ 在第一页上显示1 t0 3项8“,在第二页上显示4到6项8”,依此类推。请帮助
$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 { ?>
<a href="products.php?cat_id=<?php echo $selectedCategoryId; ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a>
<?php }
endfor; ?>
<?php endif; ?>
答案 0 :(得分:1)
尝试:
echo "Showing ".( $page == 1 ? 1 : ($page -1) * $recordsLimit +1 )." to ".($page * $recordsLimit)." item of ".$totalProducts;
答案 1 :(得分:0)
只需使用:
<?php $firstNum = (($page-1)*$recordsLimit+1) ?>
showing <?php echo $firstNum; ?> to <?php echo $firstNum+2;?> items of <?php echo $totalProducts;?>
答案 2 :(得分:0)
为了解决Tom的代码问题,其中最后一页的“to”数字不正确,我添加了一个偏移量变量。为此,$ recordsLimit变量必须匹配查询中的“showposts”参数。 OP没有显示他的查询,所以我显示了我使用过的那个。
$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';