Zend - The PHP Company




Menus & Navigation

Add Code


simple pagination using ODBC and MS Access,  

Type: code fragment
Added by: liyam
Entered: 07/04/2005
Last modified: 04/12/2004
Rating: - (fewer than 3 votes)
Views: 7727
For those of you who does'nt know, pagination (in the Web world) is usually dividing a large list of items into several pages, and then providing a nifty link for it. You usually see pagination link displayed like this: PREV 1 2 3 4 5 NEXT


<?php

//connecting to the database using ODBC
$db=odbc_connect($dsn,$user,$pass)or die("Error: Can't Connect to Database");
 
// assigns a value how many rows we will get from each page
$limit 15;  

/* assign variable with sql query of counting how many rows are there in the database.
take note you should assign * with a table data name and table_name with table name*/
$query_count    "SELECT COUNT(*) as value FROM table_name";

//executes the sql statement to the database
$result_count   odbc_exec($db$query_count); 

// get the result count from the database
$totalrows  odbc_fetch_object($result_count);   

if(empty(
$page)){    // Checks if the $page variable is empty (not set)
        
$page 1;      // If it is empty, we're on page 1
    
}

//this will be the lowest limit
$limitvalue $page $limit - ($limit);
$limitnew $limitvalue $limit;
 
// Ex: (2 * 25) - 25 = 25 <- data starts at 25 
$query "SELECT * FROM table_name where ID between $limitvalue and $limitnew ORDER BY ID ASC";

//executes the sql statement to the database
$result odbc_exec($db$query);

echo 
"<table><tr><td>Title1</td>
              <td>Title2</td>"
;

// open loop to list down contents from the database 
while (($row odbc_fetch_object($result))) {

echo 
"<tr><td>$row->content_title1</td>
               <td>
$row->content_title2</td>";

//close the loop


//now we close the table
echo "</table>";

/*check if we are at page 1. if true, PREV will not be a link. if false, $page will be a link minus 1 page. */
  
if($page != 1){
        
$pageprev $page -1;
echo(
"&nbsp;<b><a href="$PHP_SELF?page=$pageprev">PREV</a></b>&nbsp;");
    }else{
        echo(
"&nbsp;PREV&nbsp;");
    }

/* $numofpages will be the quotient of the total rows divided by the limit value
in this example lets assume that we have 47 rows in the database divided by 15 will result 3.13 */
   
$numofpages $totalrows->value $limit;

/* now let us generate those page numbers*/
    
for($i 1$i <= $numofpages; ++$i){
        if(
$i == $page){
            echo(
"&nbsp;[$i]&nbsp;");  
        }else{
            echo(
"&nbsp;<b><a href="$PHP_SELF?page=$i">$i</a></b>&nbsp;");
        }
    }

/* check if we have a remainder and print it at the end page list */
    
if(($totalrows->increm $limit) != 0){
        if(
$i == $page){
            echo(
"&nbsp;[$i]&nbsp;");
        }else{
            echo(
"&nbsp;<b><a href="$PHP_SELF?page=$i">$i</a></b>&nbsp;");
        }
    }

/* finally we check the "next" link if we have a page to move on to, if yes we put it as a link adding another page. 
in this example assuming that we have $totalrows->value=47 
this will check if (47 - (15 * 1) = 32) > zero, results to true will give a link.*/
if(($totalrows->value - ($limit $page)) > 0){
         
$pagenext $page +1;
         
        echo(
"&nbsp;<b><a href="$PHP_SELF?page=$pagenext">NEXT</a></b> ");
    }else{
        echo(
"&nbsp;NEXT&nbsp;");
    } 
 
// now let us free the resources
odbc_free_result($result);

?>


Usage Example




Rate This Script





Search



This Category All Categories