Send this link to a friend by e-mail  |  Print this Web page  |  HOME  |  SCRIPTS  |  TUTORIALS  |  BACK  |  FORWARD  |  BOTTOM 

Paging records scripts in PHP scripting language


PHP

Download world.sql ( SQL file for 'World' database )



<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->
<?php
/*
This paging records scripts example uses 'world.sql' database supplied by MySQL.
Download it from http://downloads.mysql.com/docs/world.sql.
Recommended not to save in HTML format by opening in a browser, rather download it.
For example, right click and 'Save Link As' to link provided in this page.
We use 'city' table of 'world' database, it containing 4079 records.

If we let 20 records to display at a time on a single page then,
There are 203 result pages having 20 records each, last 204th page having remaining 19 records.
Should we display 204 anchor links to 204 result pages on every result page?
If we display 15 anchor links to 15 result pages on a page then,
There are 13 such sets having 15 links each, last 14th set having remaining 9 links.
*/

class Paging
{
var $host;
var $user;
var $passwd;
var $db;
var $table;

var $query;
var $result;
var $row;

var $query_records_count;
var $result_records_count;
var $row_records_count;

var $total_records;

var $php_self;

// Maximum ( data ) records to display at a time on a single result page
var $max_r_p_p;

// Maximum anchor links ( at top ) to result pages to display at a time on a single result page
var $max_p_p_p; // Display result pages: 1, 2, 3, ... 14, 15

/*
For example 4079/20 means 204 result pages:
203 result pages having 20 records each, last 204th result page having remaining 19 records
*/
var $total_pages;

/*
Default result page to display
Atleast 1st result page is displayed if database table is not empty
*/
var $page;

/*
Whether to show? next set of result pages
Takes values like 1, 2, 3, 4, 5, ...
First time, 1st result page is displaying, so $show_next is 1
*/
var $show_next;

/*
$pages_set determines whether to change? next set of result pages
And from which result page? to?
First time, results are displaying from 1st page, so $show_next is 1
Takes values like 1, 16, 31, 46, 61, ...
*/
var $pages_set;

/*
$start and $end are used in LIMIT while querying database table
$start changes according to user request,
$start takes values like 0, 20, 40, ..
$end always equals maximum records to display at a time on a single result page,
Unless and if, there are less records left into database table on last result page
*/
var $start;
var $end;

/*
$previous ( << ) and $next ( >> ) are also anchor links ( at top ) to result pages
If there are many many records and many many result pages containing result ( data ) records
*/
var $previous;
var $next;
var $tip_start;
var $tip_end;

function Paging()
{
$this->host = 'localhost';
$this->user = 'root';
$this->passwd = 'admin';
$this->db = 'world';
$this->table = 'city';

$this->php_self = $_SERVER['PHP_SELF'];

$this->max_r_p_p = 20;
$this->max_p_p_p = 15;

$this->page = 1;
$this->show_next = 1;
$this->pages_set = 1;

$this->start = 0;
$this->end = $this->max_r_p_p;

$this->previous = NULL;
$this->next = NULL;
$this->tip_start = NULL;
$this->tip_end = NULL;
}

function connection()
{
mysql_connect($this->host, $this->user, $this->passwd);
mysql_select_db($this->db);
}

function count_records()
{
$this->query_records_count = 'SELECT count(\'id\') AS total_records FROM ' . $this->table;
$this->result_records_count = mysql_query($this->query_records_count);
$this->row_records_count = mysql_fetch_array($this->result_records_count);
$this->total_records = $this->row_records_count['total_records'];
$this->total_pages = ceil($this->total_records/$this->max_r_p_p);
}

function query_db()
{
/*
$start and $end are used in LIMIT while querying database table
$start changes according to user request,
$start takes values like 0, 20, 40, ..
$end always equals maximum records to display at a time on a single result page,
Unless and if, there are less records left into database table on last result page
*/
$this->start = ($this->page - 1) * $this->max_r_p_p; // Offset of first row to return, for 1st result page, $start is 0
if($this->end * $this->page > $this->total_records)
{
$this->end = $this->total_records; // Maximum number of rows to return ( row_count )
}
if($this->query)
{
$this->query .= ' LIMIT ' . $this->start . ', ' . $this->end;
}
else
{
$this->query = 'SELECT name, countrycode, district FROM ' . $this->table . ' ORDER BY name LIMIT ' . $this->start . ', ' . $this->end;
}
$this->result = mysql_query($this->query);
}

function display_start_link()
{
// Provide 'Start', a link to 1st result page to user
echo '<a href="' . $this->php_self . '?page=1">Start</a>&nbsp;&nbsp;';
}

function display_end_link()
{
// Provide 'End', a link to last result page to user
echo '<a href="' . $this->php_self . '?page=' . $this->total_pages . '">End</a>';
}

function display_previous_link()
{
// Display previous ( << ) link if need be
if($this->pages_set > 1)
{
$this->previous = $this->pages_set - 1; // If user sets $pages_set = 16, then $previous = 15
/*
If user sets $previous = 15, then $tip_start = 1
Already assumed $max_p_p_p = 15, we display 15 anchor links to 15 different result pages: 1, 2, 3, ... 14, 15
*/
$this->tip_start = $this->previous - $this->max_p_p_p + 1;
$this->tip_end = $this->previous; // If user sets $previous = 15, then $tip_end = 15
echo '<a href="' . $this->php_self . '?page=' . $this->previous . '" title="Pages: ' . $this->tip_start . '&ndash; ' . $this->tip_end . '">&lt;&lt;</a>&nbsp;&nbsp;';
}
}

function display_next_link()
{
/*
Display next ( >> ) link if need be
$j will get value from above loop, like 16, 31, 46, 61, ...
Assuming $max_p_p_p = 15, we display 15 anchor links to 15 different result pages: 1, 2, 3, ... 14, 15
*/
if( ($this->j-1)%$this->max_p_p_p == 0 && $this->j <= $this->total_pages )
{
$this->next = $this->j - 1; // If user sets $j = 16, then $next = 15
$this->next += 1; // $next becomes one more, that is, 16 now
$this->tip_start = $this->next; // If user sets $next = 15, then $tip_start = 16
$this->tip_end = $this->next + $this->max_p_p_p - 1; // Assuming $max_p_p_p = 15, then $tip_end goes like 30, 45, 60, ...
if($this->tip_end > $this->total_pages)
{
$this->tip_end = $this->total_pages;
}
echo '<a href="'. $this->php_self . '?page=' . $this->next . '" title="Pages: ' . $this->tip_start . ' &ndash; '. $this->tip_end . '">&gt;&gt;</a>&nbsp;&nbsp;';
}
}

function display_anchor_links_to_result_pages()
{
// Display anchor links to result pages according to $max_p_p_p and user request
for($this->j = $this->pages_set; $this->j < $this->pages_set+$this->max_p_p_p && $this->j < $this->total_pages+1; $this->j++)
{
echo '<a href="' . $this->php_self . '?page=' . $this->j . '">' . $this->j . '</a>&nbsp;&nbsp;';
}
}

function display_records()
{
// Display records on current result page
while($this->row = mysql_fetch_array($this->result))
{
echo $this->row['name'] . ', ' . $this->row['countrycode'] . ', ' . $this->row['district'] . '<br>';
}
}

function display_paging_information()
{
/*
$start and $end here are changed for user display
This is not same as above and what is used in LIMIT, when querying database table
$start and $end changes according to user request,
$start takes values like 1, 21, 41, ...
$end takes values like 20, 40, 60, ...
*/
$this->start += 1;
$this->end = $this->start + $this->max_r_p_p - 1;
if($this->end > $this->total_records)
{
$this->end = $this->total_records;
}
echo 'Displaying ' . $this->start . ' to ' . $this->end . ' of total ' . $this->total_records . ' records';
if($this->total_pages > 1)
{
echo ', ' . $this->page . ' of total ' . $this->total_pages . ' pages';
}
}

function page()
{
$this->connection();
$this->count_records();
// If total records are greater than maximum records to display at a time on a single result page:
if($this->total_records > $this->max_r_p_p)
{
/*
Determine whether to change set of anchor links to result pages:
Suppose we display 15 anchor links to 15 different result pages: 1, 2, 3, ... 14, 15
User requests 16th result page, then we need change set of anchor links to result pages: 16, 17, 18, ... 29, 30
*/
$this->show_next = ceil($this->page/$this->max_p_p_p); // For example, ceil(16/15) equals 2
$this->pages_set = ($this->show_next - 1) * 15 + 1; // User is asking next set of result pages, if $show_next = 2 and $max_r_p_p = 15, then $pages_set = 16
}
$this->query_db();
// If there is a result set then do things, do nothing otherwise
if($this->result)
{
// If there are many many result pages, we need make sets of result pages:
if($this->total_pages > $this->max_p_p_p)
{
$this->display_start_link();
$this->display_previous_link();
$this->display_anchor_links_to_result_pages();
$this->display_next_link();
$this->display_end_link();
echo '<br><br>';
$this->display_records();
echo '<br>';
$this->display_paging_information();
}
}
}
}
?>


<?php
if($_REQUEST['page'])
{
$result_page = $_REQUEST['page'];
}
else
{
$result_page = 1;
}
$paging = new Paging();
$paging->query = 'SELECT name, countrycode, district FROM city ORDER BY name';
$paging->max_r_p_p = '20';
$paging->page = $result_page; // Accept which? result page to display from user
$paging->page();
?>


<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->
[an error occurred while processing this directive]

<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->



Contribution is welcome, submit scripts or codes to mediasworks.org.

Donate for a cause


Technology Simplified!

Send this link to a friend by e-mail  |  TECHNOLOGY FORUMS  |  BACK  |  FORWARD  |  TOP  
Home | Theme | Contact | Feedback | E-mail | Advertise | Help | Privacy |   SiteMap