Quick Search:
 
 PHP Code: Pagination Example #1 Jump to:  
Category: >> PHP Code >> Pagination Example #1  

<< lastnext >>

Snippet Name: Pagination Example #1

Description: Simple pagination example. Easily modified for more functionality.

Also see:
» Pagination Example #2

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?PHP
 
IF (ISSET($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} ELSE {
   $pageno = 1;
} // if
 
// 2. Identify how many database rows are available
// This code will count how many rows will satisfy the current query.
 
$query = "SELECT count(*) FROM trv_users WHERE user_id <> ''";
$result = mysql_query($query, $db) or TRIGGER_ERROR("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
 
PRINT "NUMROWS: $numrows<br>";
 
 
// 3. Calculate number of $lastpage
// This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.
 
$rows_per_page = 5;
$lastpage = CEIL($numrows/$rows_per_page);
 
// 4. Ensure that $pageno is within range
// This code checks that the value of $pageno is an integer between 1 and $lastpage.
 
$pageno = (int)$pageno;
IF ($pageno < 1) {
   $pageno = 1;
} ELSEIF ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
 
 
// 5. Construct LIMIT clause
// This code will construct the LIMIT clause for the sql SELECT statement.
 
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
 
 
// 6. Issue the database query
// Now we can issue the database qery and process the result.
 
$query = "SELECT * FROM trv_users $limit";
$result = mysql_query($query, $db) or TRIGGER_ERROR("SQL", E_USER_ERROR);
 
PRINT "RESULT: $result<br>";
 
     PRINT <<<EOM
     <center><table bgcolor='#000000' width='90%' cellpadding="1" cellspacing="1">
     <tr align="center" bgcolor="$headcol">\n
     <td valign="middle">$user_info_username</a></td>\n
     <td valign="middle">$user_info_curr_score</td>\n               
     <td valign="middle">$user_info_high_score</td>\n
     <td valign="middle">$user_info_low_score</td>\n
     <td valign="middle">$user_info_avg_score</td>\n
     <td valign="middle">$user_info_total_tests</td>\n
     <td valign="middle">$user_info_member_since</td>\n
     <td valign="middle">$user_info_member_id</td>\n
     </tr>
 
EOM;
 
//... process contents of $result ...
 
     WHILE ($row = mysql_fetch_array($result)) {
 
     IF($rowcol == "#dddddd"){$rowcol = "#eeeeee";}ELSE{$rowcol = "#dddddd";}
 
     $since_date = DATE("M d, Y", $row[signup_date]);
 
     PRINT <<<EOM
     <tr bgcolor="$rowcol">\n
     <td align="center" valign="middle"><a href="?act=member&user_id=$row[user_id]">$row[username]</a></td>\n
     <td valign="middle">$row[current_score]</td>\n               
     <td valign="middle">$row[high_score]</td>\n
     <td valign="middle">$row[low_score]</td>\n
     <td valign="middle">$row[avg_score]</td>\n
     <td valign="middle">$row[total_tests]</td>\n
     <td valign="middle">$since_date</a></td>\n
     <td valign="middle">$row[user_id]</a></td>\n
     </tr>
EOM;
               }
 
 
          PRINT <<<EOM
 
          </td></tr></table>
          <br>
EOM;
 
 
 
 
// 7. Construct pagination hyperlinks
// Finally we must construct the hyperlinks which will allow the user to select 
// other pages. We will start with the links for any previous pages.
 
IF ($pageno == 1) {
   ECHO " FIRST PREV ";
} ELSE {
   ECHO " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   ECHO " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if
 
 
// Next we inform the user of his current position in the sequence of available pages.
 
ECHO " ( Page $pageno of $lastpage ) ";
 
// This code will provide the links for any following pages.
 
IF ($pageno == $lastpage) {
   ECHO " NEXT LAST ";
} ELSE {
   $nextpage = $pageno+1;
   ECHO " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   ECHO " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if
 
?>


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org