Can anybody point me in the direction of a VERY easy to implement PHP Paginating script?
Many thanks![]()

Can anybody point me in the direction of a VERY easy to implement PHP Paginating script?
Many thanks![]()

#define('paginating'=?)


What are you paginating? MySQL? LDAP? MSSQL? etc...
If it's MySQL you're taking your data from, the LIMIT clause may be very useful to you.
The while loop is pretty generic and won't make things nice, but you (hopefully) get the idea.PHP Code:<?php
$perPage = 20;
$pageNum = (int)$_GET['pagenum'];
if( $pagenum < 0 ) { $pageNum = 0; }
$from = $pageNum * $perPage;
$query = 'SELECT * FROM my_table LIMIT '.$from.', '.$perPage;
// mysql connection stuffs...
$data = mysql_query($query);
echo '<table>';
while( $row = mysql_fetch_assoc($data) ) {
echo '<tr><td>';
var_dump($row);
echo '</td></tr>';
}
echo '</table>';
?>
Hello
:-Dave
As always, code is untested and has no guarantees of any kind.
If you're grabbing from MySQL or suchlike, use the limit function of SQL... easiest and simplest way!![]()

SELECT * FROM table WHERE thing=true LIMIT 10;
gets the first ten records returned only
SELECT * FROM table WHERE thing=true LIMIT 10, 10;
gets ten records from row eleven onwards (note: add an ORDER BY if you want to be able to predict what you'll be getting).
you can always create a chunk of php code around your main loop to return pages of information - e.g. off the top of my head (from things I've done in the past):
----------------------------------
$sql = "SELECT * FROM mytable ";
if(($pageStart == "")||($pageStart < 0)){
$pageStart = 0;
}
$sql .= "LIMIT $pageStart,50";
$result = @mysql_query($sql);
while($resultArray = @mysql_fetch_array($result)){
// Grab a new row of data
// Do your grab / display routine here
$pageStart++;
}
// To create a google-esque page list
$sql = "SELECT * FROM tablename";
$PageNum = 0; // Set First Page
$numPages = 0; // Set zero number of pages to display
$result = @mysql_query($sql);
$numresults = @mysql_num_rows($result);
while($numPages <= $numresults){
print "<a href=\"thisscriptpage.pgp&pageStart=$numPages\"> $PageNum </a>";
$PageNum++;
$numPages = $numPages + 50;
}
------------------------------
This will create a page with fifty entries per page, if you enter the page with no starting point it will display from the beginning...
hope that helps a bit - PM me if you need a bit more info.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)