472,090 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,090 software developers and data experts.

I need pagination

Hi,

I have been looking around for a way to paginate php/mysql table output.
There are a lot of hits when I google it. But unfortunately what I have
tried either does not work or is imho just arcane code.

So I am looking for a simple, clearly explained, and working pagination
code snip that I can adapt, or a modular solution. I'd prefer something
you have tried yourself and know it works. The simpler the better.
Thanks very much.

Lee G.
Jul 17 '05 #1
5 2258
I was looking for a similar thing a while back, and then I found this.
Works for me:

<?php

/* ================================================== ==================
* Copyright (c) 2000 Steven Haryanto. All rights reserved.
*
* StoryPager
* A PHP module to do automatic story paging
*
* This module is released under the GNU General Public License. See:
* http://www.gnu.org/copyleft/gpl.html
*
* Version
* 0.011, Wed Dec 13 03:24:45 2000
*
* For latest version and example, visit:
* http://steven.haryan.to/php/StoryPager.html
*
* ================================================== ==================
*
*/

class StoryPager {

function StoryPager() {
// default values
$this->nb_tag = "<!-- NOBREAK -->";
$this->mb_tag = "<!-- BREAK -->";
$this->even_paging = 1;
}

// whether to do even paging
function even_paging($value) {
if (isset($value)) $this->even_paging = $value;
return $this->even_paging;
}

// target number of pages
function target_nop($value) {
if (isset($value)) $this->target_nop = $value;
return $this->target_nop;
}

// optimum page length, in bytes
function optimum_pl($value) {
if (isset($value)) $this->optimum_pl = $value;
return $this->optimum_pl;
}

// string to look for to prevent breaking/paging the whole story
function nb_tag($value) {
if (isset($value)) $this->nb_tag = $value;
return $this->nb_tag;
}

// string to look for doing manual breaking
function mb_tag($value) {
if (isset($value)) $this->mb_tag = $value;
return $this->mb_tag;
}

// do the split
function split($content) {
$this->pages = array();

// should we break at all?
if (preg_match("/\Q$this->nb_tag/is", &$content)) {
push($this->pages, &$content);
return;
}

// if yes, should we manual-break?
if (preg_match("/\Q$this->mb_tag/is", &$content)) {
$this->pages = preg_split("/\Q$this->mb_tag/is", &$content);
return;
}

// if no manual break is specified, try to do automatic paging
$content_length = strlen(&$content);

if (!$this->even_paging) {
if (!$this->optimum_pl) {
die("You must specify desired page length (optimum_pl)");
}
if ($this->target_nop) {
$pl = round(strlen(&$content)/$this->target_nop);
} else {
$pl = $this->optimum_pl;
}
} else {

if ($this->target_nop) {
$nop = $this->target_nop;
} else if ($this->optimum_pl) {
$nop = round($content_length/$this->optimum_pl);
} else {
die("At least specify target number of pages (target_nop) ".
"or optimum page length (optimum_pl)");
}
if ($nop == 0) $nop=1;
$pl = round($content_length/$nop);

}

$lines = explode("\n", &$content);
$in_table = 0;
$in_pre = 0;
$offset = 0;
$cur_offset = 0;
$distance = 0;
$index = 0;
$this->pages[$index] = '';

foreach ($lines as $line) {
$linebr = $line . "\n";
$len = strlen(&$linebr);

$p_tag=0;

$p_tag = preg_match("/<\/?p>/i", &$line);
preg_match_all("/<pre>/i", &$line, $m); $in_pre +=
sizeof($m[0]);
preg_match_all("/<table/i", &$line, $m); $in_table +=
sizeof($m[0]);
$offset += $len;

// is it time to start a new page?
if (
$in_pre <= 0 && $in_table <= 0 &&
($p_tag || !preg_match("/\S/", $line)) &&
(!$this->target_nop || $index+1 < $this->target_nop) &&
($cur_offset > 0.75*$pl) && // don't let a page be too short
$offset > $pl*($index+1)
) {
++$index;
$this->pages[$index] = $linebr;
$cur_offset = 0;

} else { // nope, keep writing to the current page

$this->pages[$index] .= $linebr;
$cur_offset += $len;

}

preg_match_all("/<\/pre>/i", &$line, $m); $in_pre -=
sizeof($m[0]);
preg_match_all("/<\/table>/i", &$line, $m); $in_table -=
sizeof($m[0]);

}

// remove last page if empty
if (!preg_match("/\S/", $this->pages[$index]))
array_pop($this->pages);
if ($this->pages[$index] = "</p>") {
$this->pages[$index-1] .= "</p>";
array_pop($this->pages);
}
}

// retrieve the number of pages
function nop() {
return sizeof($this->pages);
}

// retrieve the pages
function &page($index) {
return $this->pages[$index-1];
}

} // class

?>
Jul 17 '05 #2
The best way to make a simple query-result pagination is using the
LIMIT parameter in the SQL query.

I'll write a simple script that will extract some records from the
table "table1".

<?php
include('config.inc.php'); //configuration file containing database
connection params
$db = mysql_connect($db_host, $db_user, $db_password) or die
("Error");
mysql_select_db($db_name, $db) or die ("Error");

$count_query = mysql_query("SELECT COUNT(id) FROM table1"); //Returns
the total number of the table records

$count_result = mysql_fetch_row($count_query);

$tot_records = $count_result[0]; //Total number of the records

$records_per_page = 10;

$tot_pages = ceil($tot_records / $records_per_page); //Calculates the
total number of the pages. ceil() function rounds the result of the
division to the nearest integer

$current_page = (!$_GET['page']) ? 1 : (int)$_GET['page']; //If the
page is the first (so without $_GET parameters sent) the current page
is 1

$first_record = ($current_page - 1) * $per_page; //The first record to
be shown

$records_query = mysql_query("SELECT id, name FROM table1 LIMIT
$first_record, $records_per_page");

while ($results = mysql_fetch_array($records_query)) {
echo "<p>" . $results ['nome'] . "</p>";
}

//Now you have to make the navigation bar
echo "page -";
for ($i=1;$i<=$tot_pages;$i++) { // This will print "page - 1 - 2 - 3
- n -"
if ($i == $current_page) { //If the number is the current page
no link is made...
echo $i . " - ";
}
else { //...else the number links to the n^th page
echo "<a href='this_page.php?page=" . $i . "'>" . $i .
"</a> - ";
}
}

?>
That's all I think, but I didn't check for any errors in the script,
as i made it at the moment. I just wanted to show you the simplest way
(IMHO) to handle records pagination so forgive any bugs :-)
Jul 17 '05 #3
The website for this is a deadlink.

somaBoy MX wrote:
I was looking for a similar thing a while back, and then I found this.
Works for me:

<?php

/* ================================================== ==================
* Copyright (c) 2000 Steven Haryanto. All rights reserved.
*
* StoryPager
* A PHP module to do automatic story paging
*
* This module is released under the GNU General Public License. See:
* http://www.gnu.org/copyleft/gpl.html
*
* Version
* 0.011, Wed Dec 13 03:24:45 2000
*
* For latest version and example, visit:
* http://steven.haryan.to/php/StoryPager.html
*
* ================================================== ==================
*
*/

class StoryPager {

function StoryPager() {
// default values
$this->nb_tag = "<!-- NOBREAK -->";
$this->mb_tag = "<!-- BREAK -->";
$this->even_paging = 1;
}

// whether to do even paging
function even_paging($value) {
if (isset($value)) $this->even_paging = $value;
return $this->even_paging;
}

// target number of pages
function target_nop($value) {
if (isset($value)) $this->target_nop = $value;
return $this->target_nop;
}

// optimum page length, in bytes
function optimum_pl($value) {
if (isset($value)) $this->optimum_pl = $value;
return $this->optimum_pl;
}

// string to look for to prevent breaking/paging the whole story
function nb_tag($value) {
if (isset($value)) $this->nb_tag = $value;
return $this->nb_tag;
}

// string to look for doing manual breaking
function mb_tag($value) {
if (isset($value)) $this->mb_tag = $value;
return $this->mb_tag;
}

// do the split
function split($content) {
$this->pages = array();

// should we break at all?
if (preg_match("/\Q$this->nb_tag/is", &$content)) {
push($this->pages, &$content);
return;
}

// if yes, should we manual-break?
if (preg_match("/\Q$this->mb_tag/is", &$content)) {
$this->pages = preg_split("/\Q$this->mb_tag/is", &$content);
return;
}

// if no manual break is specified, try to do automatic paging
$content_length = strlen(&$content);

if (!$this->even_paging) {
if (!$this->optimum_pl) {
die("You must specify desired page length (optimum_pl)");
}
if ($this->target_nop) {
$pl = round(strlen(&$content)/$this->target_nop);
} else {
$pl = $this->optimum_pl;
}
} else {

if ($this->target_nop) {
$nop = $this->target_nop;
} else if ($this->optimum_pl) {
$nop = round($content_length/$this->optimum_pl);
} else {
die("At least specify target number of pages (target_nop) ".
"or optimum page length (optimum_pl)");
}
if ($nop == 0) $nop=1;
$pl = round($content_length/$nop);

}

$lines = explode("\n", &$content);
$in_table = 0;
$in_pre = 0;
$offset = 0;
$cur_offset = 0;
$distance = 0;
$index = 0;
$this->pages[$index] = '';

foreach ($lines as $line) {
$linebr = $line . "\n";
$len = strlen(&$linebr);

$p_tag=0;

$p_tag = preg_match("/<\/?p>/i", &$line);
preg_match_all("/<pre>/i", &$line, $m); $in_pre +=
sizeof($m[0]);
preg_match_all("/<table/i", &$line, $m); $in_table +=
sizeof($m[0]);
$offset += $len;

// is it time to start a new page?
if (
$in_pre <= 0 && $in_table <= 0 &&
($p_tag || !preg_match("/\S/", $line)) &&
(!$this->target_nop || $index+1 < $this->target_nop) &&
($cur_offset > 0.75*$pl) && // don't let a page be too short
$offset > $pl*($index+1)
) {
++$index;
$this->pages[$index] = $linebr;
$cur_offset = 0;

} else { // nope, keep writing to the current page

$this->pages[$index] .= $linebr;
$cur_offset += $len;

}

preg_match_all("/<\/pre>/i", &$line, $m); $in_pre -=
sizeof($m[0]);
preg_match_all("/<\/table>/i", &$line, $m); $in_table -=
sizeof($m[0]);

}

// remove last page if empty
if (!preg_match("/\S/", $this->pages[$index]))
array_pop($this->pages);
if ($this->pages[$index] = "</p>") {
$this->pages[$index-1] .= "</p>";
array_pop($this->pages);
}
}

// retrieve the number of pages
function nop() {
return sizeof($this->pages);
}

// retrieve the pages
function &page($index) {
return $this->pages[$index-1];
}

} // class

?>

Jul 17 '05 #4
Thanks but I looking for working code. It's really hard to find
a pager that works it's impossible in Perl and seems like the same deal
in PHP too - why i don't know.
Shawn wrote:
The best way to make a simple query-result pagination is using the
LIMIT parameter in the SQL query.

I'll write a simple script that will extract some records from the
table "table1".

<?php
include('config.inc.php'); //configuration file containing database
connection params
$db = mysql_connect($db_host, $db_user, $db_password) or die
("Error");
mysql_select_db($db_name, $db) or die ("Error");

$count_query = mysql_query("SELECT COUNT(id) FROM table1"); //Returns
the total number of the table records

$count_result = mysql_fetch_row($count_query);

$tot_records = $count_result[0]; //Total number of the records

$records_per_page = 10;

$tot_pages = ceil($tot_records / $records_per_page); //Calculates the
total number of the pages. ceil() function rounds the result of the
division to the nearest integer

$current_page = (!$_GET['page']) ? 1 : (int)$_GET['page']; //If the
page is the first (so without $_GET parameters sent) the current page
is 1

$first_record = ($current_page - 1) * $per_page; //The first record to
be shown

$records_query = mysql_query("SELECT id, name FROM table1 LIMIT
$first_record, $records_per_page");

while ($results = mysql_fetch_array($records_query)) {
echo "<p>" . $results ['nome'] . "</p>";
}

//Now you have to make the navigation bar
echo "page -";
for ($i=1;$i<=$tot_pages;$i++) { // This will print "page - 1 - 2 - 3
- n -"
if ($i == $current_page) { //If the number is the current page
no link is made...
echo $i . " - ";
}
else { //...else the number links to the n^th page
echo "<a href='this_page.php?page=" . $i . "'>" . $i .
"</a> - ";
}
}

?>
That's all I think, but I didn't check for any errors in the script,
as i made it at the moment. I just wanted to show you the simplest way
(IMHO) to handle records pagination so forgive any bugs :-)

Jul 17 '05 #5
Tony Marston wrote:
Take a look at http://www.tonymarston.co.uk/php-mysql/pagination.html

Thanks for all the help.
I found this to clear and it worked:

http://www.phpnoise.com/tutorials/9/2
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Sharif T. Karim | last post: by
1 post views Thread by Kruq | last post: by
2 posts views Thread by Chris H | last post: by
1 post views Thread by dhanu | last post: by
1 post views Thread by shalini jain | last post: by
2 posts views Thread by kkshansid | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.