2012-03-08 52 views
0

这里的分页工作正常,但除了分页,因为每页的限制是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; ?> 

回答

1

尝试:

echo "Showing ".($page == 1 ? 1 : ($page -1) * $recordsLimit +1)." to ".($page * $recordsLimit)." item of ".$totalProducts; 
+0

谢谢:)这个工作得很好,我在检查你的答案前自己找到了解决方案,那就是 显示<?php echo($ pageNumber + 1)。'至 '。 $ page * $ recordsLimit。' '。$ totalProducts。'产品; ?> 但问题出现在第3页,它显示“显示8到7中的7到9 – TPSstar 2012-03-08 20:18:00

0

只需使用这样的: <?php $firstNum = (($page-1)*$recordsLimit+1) ?> showing <?php echo $firstNum; ?> to <?php echo $firstNum+2;?> items of <?php echo $totalProducts;?>

+0

你的答案几乎是正确的但是如果他突然想改变recordsLimit呢?那么他也必须手动将“$ firstNum + 2”更改为别的。 – Ignas 2012-03-08 20:09:27

0

要与Tom的代码解决这个问题,其中最后一页有一个不正确“到”数我增加了一个偏移变量。为此,$ 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';