Jump to content

Echo showing results in pagination page


jacko_162

Recommended Posts

i have built pages that paginate with 10 rows per page (some pages show more but for the moment i want to focus on this particular page)

//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 toonCategory='Capital' AND 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;
}

The above sets the variables for the pagination and below the results are echo with 10 rows per page

 

then i show the pagination links:

<?php 
$sql = "SELECT * FROM `ecmt_memberlist` WHERE toonCategory='Capital' AND 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='capitalmember.php?page=1'>".'«'."</a> ";?> 
</li>
<?
for ($i=1; $i<=$total_pages; $i++) { 
            echo "<li><a href='capitalmember.php?page=".$i."'>".$i."</a></li> "; 
};
?>
<li class="selected">
<? 
echo "<a href='capitalmember.php?page=$total_pages'>".'»'."</a> "; // Goto last page
?></li>
  </ul></div>
    <?php
  $pageNr = $page;   // Get Current Page Number
$from = $pageNr * $rowsPerPage;  // 3 * 10 = 30  // 3 * 10 = 30
$to = $from + $rowsPerPage;      // 30 + 10 = 40
echo $pageNr;
/* Result: From page 30 to 40 */
  ?></td>
        </tr>
      </table>

this works great and shows me 10 results per page,

 

what i need to work out and work on next is:

 

echo the number of results above the records (for example: "showing records 1 to 10" and then on page 2 "showing records 11 to 21" and page 3 "showing records 22 to 32"

 

how can i work out the maths for this echo?

 

i was thinking along the lines of;

    <?php
$pageNr = $page;   // Gets Current Page Number
$from = $pageNr * $rowsPerPage;  // 3 * 10 = 30  
$to = $from + $rowsPerPage;      // 30 + 10 = 40
// Now Show Results
echo $from; // Echo from
echo $to // Echo to
  ?>

but i'm still working on this..

 

then i need to look at shortening the amount of page links on the page if for example i have 500 records it shows me links 1 to 50 and eats up the page....

 

Appreciate any help and light onto my problems.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/293856-echo-showing-results-in-pagination-page/
Share on other sites

ok so i did this;

 

    <?php
$pageNr = $page;   // Get Current Page Number
$from = $pageNr * $num_rec_per_page - 9;  // 3 * 10 = 30  
$to = $from + 9;      // 30 + 9 = 40
// ECHO results Statements
echo "Showing Results " .$from. " to " .$to. " ";
?>
 
which works, but the last page may not be equal to exactly 10,
 
for example 47 total results on page 4 it will only show 7 rows,
 
so i did an if statement to check if its the last page and assign different math to the variable. how do i work out the last number??
 

    <?php$pageNr = $page;   // Get Current Page Number
$from = $pageNr * $num_rec_per_page - 9;  // 3 * 10 = 30  


//check if last page and change variables
if (isset($_GET['page']) && $_GET['page'] == $total_pages) {
$to = $count / $num_rec_per_page;
} else {
$to = $from + 9;
}


// ECHO results Statements
echo "Showing Results " .$from. " to " .$to. " ";
?>

you know the total number of rows in $total_records. you can simply limit the $to value to that. 

$to = min($total_records,$to); // use the smaller value in $total_records or in $to.

as to how to limit the number of pagination links that are displayed around the currently selected page, see the phpfreaks,com main site pagination tutorial - http://www.phpfreaks.com/tutorial/basic-pagination specifically, see the $range variable and how it is used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.