0

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

Stephen Jackson
  • 260
  • 2
  • 6
  • 20

1 Answers1

1

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

manuelbcd
  • 3,106
  • 1
  • 26
  • 39
  • problem im having is getting my head around: page #1 shows records ~1 to ~10 page #2 shows records ~11 to ~21 page #3 shows records ~22 to ~32 page #4 shows records ~33 to ~43 page #5 shows records ~44 to ~47 (only showing 3 records atm!) – Stephen Jackson Jan 11 '15 at 22:08
  • 1
    Show 1 to 10, 11 to 20, 21 to 30, and for last page use MOD operation: TotalPages % $rowsPerPage. For example 47 mod 10 = 7 – manuelbcd Jan 11 '15 at 22:29
  • sorry i dont understand – Stephen Jackson Jan 11 '15 at 22:31
  • 1
    Excuse me, I was not explaying well. I mean: first 10 records are 1 to 10, second 10 records are 21 to 20, and again... until the last page. And to know last page records you only have to apply mod to total results. If total results are 47 and you have 10 records pages.. 47 % 10 = 7. Your last page has 7 records. – manuelbcd Jan 11 '15 at 22:49
  • but how can i modify the code to know when its on the last page? something along the lines of if $_Get["page"] == $total_pages // IF Get page is equal to total pages variable do different maths equation for results on last page – Stephen Jackson Jan 11 '15 at 23:10
  • Please check this answer, I have reviewed some ones and I think is the best, it shows all that you want: http://stackoverflow.com/questions/20364349/multiple-pages-using-mysql-limit-offset – manuelbcd Jan 11 '15 at 23:26