Display N records per page

Hi,

I am pretty new in PHP, I can already display, add, edit, delete records. But the thing is I dont know how to diplay N records in one page with the <<prev [1][2][3][4] next in PHP. Can anyone help me with this? Thank you very much!

Use a for loop, passing to the page a var to know where to start. Lets assume you want 20 records per page:


if (!$start || $start < 0) { $start = 0 }

for ($i = $start; $i <= $start+20; $i++) {
     // code to display a single record
}

echo "<a href='script.php?start=".$start-20.">prev";

etc … the logic of how to do the others should follow. also, don’t forget to check your variables are safe from malicious users and such.

Here is a class i got from this forum, which i forgot the link where i got it.



<?php
/**************************************************************************************
* Class: Pager
* Author: Tsigo <tsigo@tsiris.com>
* Methods:
*         findStart
*         findPages
*         pageList
*         nextPrev
* Redistribute as you see fit.
**************************************************************************************/
class Pager
  {
  /***********************************************************************************
   * int findStart (int limit)
   * Returns the start offset based on $_GET['page'] and $limit
   ***********************************************************************************/
   function findStart($limit)
    {
     if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
      {
       $start = 0;
       $_GET['page'] = 1;
      }
     else
      {
       $start = ($_GET['page']-1) * $limit;
      }

     return $start;
    }
  /***********************************************************************************
   * int findPages (int count, int limit)
   * Returns the number of pages needed based on a count and a limit
   ***********************************************************************************/
   function findPages($count, $limit)
    {
     $pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;

     return $pages;
    }
  /***********************************************************************************
   * string pageList (int curpage, int pages)
   * Returns a list of pages in the format of "« < [pages] > »"
   ***********************************************************************************/
   function pageList($curpage, $pages)
    {
     $page_list  = "";

     /* Print the first and previous page links if necessary */
     if (($curpage != 1) && ($curpage))
      {
       $page_list .= "  <a href=\\"".$_SERVER['PHP_SELF']."?page=1\\" title=\\"First Page\\">«</a> ";
      }

     if (($curpage-1) > 0)
      {
       $page_list .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\\" title=\\"Previous Page\\"><</a> ";
      }

     /* Print the numeric page list; make the current page unlinked and bold */
     for ($i=1; $i<=$pages; $i++)
      {
       if ($i == $curpage)
        {
         $page_list .= "<b>".$i."</b>";
        }
       else
        {
         $page_list .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".$i."\\" title=\\"Page ".$i."\\">".$i."</a>";
        }
       $page_list .= " ";
      }

     /* Print the Next and Last page links if necessary */
     if (($curpage+1) <= $pages)
      {
       $page_list .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\\" title=\\"Next Page\\">></a> ";
      }

     if (($curpage != $pages) && ($pages != 0))
      {
       $page_list .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".$pages."\\" title=\\"Last Page\\">»</a> ";
      }
     $page_list .= "</td>\
";

     return $page_list;
    }
  /***********************************************************************************
   * string nextPrev (int curpage, int pages)
   * Returns "Previous | Next" string for individual pagination (it's a word!)
   ***********************************************************************************/
   function nextPrev($curpage, $pages)
    {
     $next_prev  = "";

     if (($curpage-1) <= 0)
      {
       $next_prev .= "Previous";
      }
     else
      {
       $next_prev .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\\">Previous</a>";
      }

     $next_prev .= " | ";

     if (($curpage+1) > $pages)
      {
       $next_prev .= "Next";
      }
     else
      {
       $next_prev .= "<a href=\\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\\">Next</a>";
      }

     return $next_prev;
    }
  }
?>


Example on how to use:


<?php
/* Instantiate class */
require_once("class.pager.php");
$p = new Pager;

/* Show many results per page? */
$limit = 100;

/* Find the start depending on $_GET['page'] (declared if it's null) */
$start = $p->findStart($limit);

/* Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query */
$count = mysql_num_rows(mysql_query("SELECT field FROM table"));

/* Find the number of pages based on $count and $limit */
$pages = $p->findPages($count, $limit);

/* Now we use the LIMIT clause to grab a range of rows */
$result = mysql_query("SELECT * FROM table LIMIT ".$start.", ".$limit);

/* Now get the page list and echo it */
$pagelist = $p->pageList($_GET['page'], $pages);
echo $pagelist;

/* Or you can use a simple "Previous | Next" listing if you don't want the numeric page listing */
//$next_prev = $p->nextPrev($_GET['page'], $pages);
//echo $next_prev;

/* From here you can do whatever you want with the data from the $result link. */
?>

This is something I use to get my [1] [2] [3] page listed.


$perpage = "20"; // 20 items per page

$result = mysql_query("select COUNT(*) FROM [table]"); 
$total = mysql_result($result, 0, 0);  
$pageCount = ($total / $perpage); 
if (!isset($HTTP_GET_VARS['page']) || $HTTP_GET_VARS['page'] < 0 || $HTTP_GET_VARS['page'] > $pageCount) {
$start = 0; } else { $start = $perpage * $HTTP_GET_VARS['page']; }  
 
 
<table width='<?=$tablewidth?>' border='0' cellspacing='10' cellpadding='0' align='center'>
 <tr>
  <td align="center"><font size='2' face='arial'>

<?php
$prev_page = $page - 1;
$next_page = $page + 1;
$first10 = $page - 10;
if($first10 < 0){ $first10="0";}

$last10 = $page + 10;
if($last10 > $pageCount){ $last10 ="$pageCount";}

if($prev_page > -1){
echo "<a href=\\"{$_SERVER['PHP_SELF']}?page=$prev_page\\"><b>PREVIOUS</a></b> | ";
}
for ($i = $first10; $i < $last10; $i++) {
if($page==$i){
echo "<font color='red' size='3'><b> $i </b></font> ";}
else { echo "&nbsp;<a href=\\"{$_SERVER['PHP_SELF']}?page=$i\\"><b><u>$i</u></b></a>&nbsp;";}
}

if($next_page < $pageCount){ 
echo " | <a href=\\"{$_SERVER['PHP_SELF']}?page=$next_page\\"><b>NEXT</a></b>";
}
echo "</font></td></tr></table>";

Hi Guys… thanks alot for the replies! I tried all your suggestions into my code but unfortunately I can make it to work, I must have made some mistakes implementing the snippets into my code…

Okay so far here’s what I’ve got…Anyone mind to pin point what I missed there? Since I set up the items to be 10 perpage and I have 11 items overall and they’re all still squeezed up into one page! Thanks alot!!!

<?php
require ‘db_connect.php’;

$perpage = “10”; // 10 items per page

$check = $db_object->query(“SELECT * FROM message INNER JOIN testsession ON message.user = testsession.username ORDER BY message.id DESC”);

$total = mysql_result($check, 0, 0);

if (DB::isError($check) || $check-&gt;numRows() == 0) {
	die('No message has been submitted so far');
}
else {
while($info = $check-&gt;fetchRow()){

$pageCount = ($total / $perpage);
if (!isset($HTTP_GET_VARS[‘page’]) || $HTTP_GET_VARS[‘page’] < 0 || $HTTP_GET_VARS[‘page’] > $pageCount) {
$start = 0; } else { $start = $perpage * $HTTP_GET_VARS[‘page’]; }
?>

<table width=“90%” border=“0” cellpadding=“2” cellspacing=“0”>
<tr>
<td width=“77%” valign=“top” bgcolor=“#00CC00”><?php print “$info[title]”; ?><br>
<?php print “$info[message]”; ?></td>
</tr>
<tr>
<td valign=“top” bgcolor=“#0099FF”> </td>
</tr>
</table>

<?php
$prev_page = $page - 1;
$next_page = $page + 1;
$first10 = $page - 5;
if($first10 < 0){ $first10=“0”;}

$last10 = $page + 5;
if($last10 > $pageCount){ $last10 =“$pageCount”;}

if($prev_page > -1){
echo “<a href=\”{$_SERVER[‘PHP_SELF’]}?page=$prev_page\"><b>PREVIOUS</a></b> | ";
}
for ($i = $first10; $i < $last10; $i++) {
if($page==$i){
echo “<font color=‘red’ size=‘3’><b> $i </b></font> “;}
else { echo " <a href=\”{$_SERVER[‘PHP_SELF’]}?page=$i\”><b><u>$i</u></b></a> ";}
}

if($next_page < $pageCount){
echo " | <a href=\“{$_SERVER[‘PHP_SELF’]}?page=$next_page\”><b>NEXT</a></b>";
}
}
}
$db_object->disconnect();
?>

ok, I missed a part of the code.

Where your query is to get the results for the page (not the number of the page) but the content, you need to have a start, end point. Like this


SELECT * FROM [table]  LIMIT $start, $perpage

PM me if you would like an example. :slight_smile: