NavyPixel
NavyPixel

Reputation: 260

echo how many records are being show on pagination page

i want to disoplay how many records are being show on each page,

for instance;

"Showing records #1 to #10" or "showing records #11 to #21"

i have the following code working showing me 10 records per page (sometimes more on different pages)

<?php
    //Define Some Options for Pagination
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page;

$results = mysql_query("SELECT * FROM `ecmt_memberlist` WHERE oldMember = 0 ORDER BY CONCAT(MainToon, Name) LIMIT $start_from, $num_rec_per_page") or die(mysql_error());

    $results_array = array();
    while ($row = mysql_fetch_array($results)) {
        $results_array[$row['characterID']] = $row;
}
?>

        <?php 
    $sql = "SELECT * FROM `ecmt_memberlist` WHERE oldMember = 0 ORDER BY CONCAT(MainToon, Name)";

    $rs_result = mysql_query($sql); //run the query
    $total_records = mysql_num_rows($rs_result);  //count number of records
    $total_pages = ceil($total_records / $num_rec_per_page); 
    ?>


              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td align="center"><div style="width:100%; text-align:center;">
                  <ul class="pagination">
            <li class="selected">
            <?php
        echo "<a href='members.php?page=1'>".'«'."</a> ";?> 
        </li>
        <?
        for ($i=1; $i<=$total_pages; $i++) { 
                    echo "<li><a href='members.php?page=".$i."'>".$i."</a></li> "; 
        };
        ?>
        <li class="selected">
        <? 
        echo "<a href='members.php?page=$total_pages'>".'»'."</a> "; // Goto last page
        ?></li>
          </ul></div></td>
                </tr>
              </table>
            </td>
          </tr>
        </table>

everything with the above code works fine but how would i display the echo "Showing records #10 to #11" etc..

pages in the url show tracking.php?page=12

many thanks for the help

Upvotes: 0

Views: 1175

Answers (1)

manuelbcd
manuelbcd

Reputation: 4527

You have to store page number from $_GET var

Calc is simple:

$pageNr = (int)$_GET['pageNr']   // For example 3
$from = $pageNr * $rowsPerPage;  // 3 * 10 = 30
$to = $from + $rowsPerPage;      // 30 + 10 = 40
/* Result: From page 30 to 40 */

Also it would be fine for you to use any 3rd party class or library, like zebra pagination: http://stefangabos.ro/php-libraries/zebra-pagination/

regards

Upvotes: 1

Related Questions