473,499 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2381
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3375
by: Sharif T. Karim | last post by:
Anyone know of a up-to-date tutorial for pagination where I can have it like: Prev 1 2 3 4 Next Thanks. -- Sharif T. Karim ....you don't know wrath yet...
1
5801
by: Kruq | last post by:
Is it possible to use pagination with DataList? Can't find it.. :( Kruq
2
2298
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
1
7396
by: dhanu | last post by:
How to manage pagination in AJAX? My requirement is similar to GMails inbox feature. Whenever a new mail comes in,the newly arrived mail is at the start of my inbox and last row in that page...
4
3628
by: Ed Jay | last post by:
I generate a DHTML page (a medical report) with dynamically generated text based on user input (answers to questions). The page length changes dynamically. I desire that when the page is printed...
1
1816
by: OceanBreeze | last post by:
I am new to .Net. I am using ASP 2.0 and C#. I want to pupolate a data grid programatically using the values obtained from a list conating domain objects. E.g., DAL.GetEmployee() returns a...
1
6833
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
4
3551
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
2
2582
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.