PHP 如何获取页面上 "showing # of # items" 中的最后一项计数

PHP how to get the last item count in "showing # of # items" on a page

我正在制作一个产品页面,在我的分页旁边我想显示每页的项目数。

所以像 "Showing 1 - 10 products"。

我想出了如何计算第一个项目编号,但我仍然无法计算页面上最后一个项目的数量。

所以假设我每页有 10 个项目,总共有 35 个项目,第一页将显示“1 - 10”,下一页将显示“11 - 20”,下一页将显示“21 - 30”,最后一页会显示“31 - 35”。

<?php
//connect to the database
include_once ('includes/connect_to_mysql.php'); 
    $category = urldecode ($_GET["cat"]); // get category for the current page from the URL
    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    $limit = 10;
    $startpoint = ($page * $limit) - $limit;
    $statement = "products WHERE active = 1 AND category = '$category'";
?>
<header>
    <div class="paging">
        Page: &nbsp; <?php 
        $sql = "SELECT * FROM products WHERE active = 1 AND category = '$category'"; 
        $rs_result = mysql_query($sql); //run the query
        $total_records = mysql_num_rows($rs_result);  //count number of records
        $num_rec_per_page = 10; //number of records per page
        $total_pages = ceil($total_records / $num_rec_per_page); 

        for ($i=1; $i<=$total_pages; $i++) { 
            echo '<a href="categories.php?cat='.urlencode($category).'&page='.$i.'">'.$i.'</a>&nbsp; | &nbsp;';
            };

        $first = ($page * $limit) - $limit +1;
        $last = 'NOT SURE WHAT TO PUT HERE';
?>
    </div>
    <form action="#" >
    <select onchange="javascript:addSort();" name="sortBy" id="sortBy">
        <option value="">Default</option>
        <option value="PriceHiLo">Price (High to Low)</option>
        <option value="PriceLoHi">Price (Low to High)</option>
        <option value="pID">Most Recent</option>
    </select> &nbsp; Showing <?php echo $first?> - <?php echo $last?> of <?php echo $total_records?> Product(s)
    </form>
</header>

任何帮助弄清楚如何在上面的代码中计算 $last 的人都将不胜感激。谢谢。

我找到了解决方案,我需要一个 if 语句:

            if ($total_records > ($page * $limit)){
            $last = ($page * $limit);
            } else {
                $last = $total_records;
            };

没有 if case

floor(total / perPageSize) + (total % perPageSize)