Hiya all,
I've been googling for few minutes now and I can't seem to find pagination explained in details. What I need is when I extract data from my database into <tr> rows of page, to display lets say only 10 of those rows from database on first page and 10 on second and so on .. now I know how to do this with LIMIT in the mysql_query .. to manually put links to page numbers like 1 2 .. next previous and so on .. but I'm sure there is a way that php does it by itself .. without me doing everything manually .. any ideas ? maybe links to good tutorials ?
Thank you
php pagination
Moderator: General Moderators
Re: php pagination
Use mine as a template, i would explain it but its noted enough so read it all....I find the best way to learn is when your given a whole example and you can implement it into your own needs aswell as read all the notes 
Obviously you can take out my example table part and change with your own =D
ALSO: you can not of looked very hard on google, it took me 30min to create a pageination system from google..
Code: Select all
// database connection info
$conn = mysql_connect(*********) or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db(*******,$conn) or trigger_error("SQL", E_USER_ERROR);
$CharacterLimit=30;
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM [color=#FF0000]TABLENAME[/color]";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 24;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT * FROM [color=#FF0000]TABLE NAME [/color]LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// START AN OUTPUT TABLE
echo "<table>";
// IF NO RESULTS
if (!mysql_num_rows($result))
{
echo "<tr><td colspan='2'>Why oh why is my php table failing??</td></tr>";
}
// IF WE HAVE RESULTS
else
{
// TITLE FOR COLUMNS
echo "<tr><td nowrap><b>example22</b></td></tr>";
// ITERATE OVER THE RESULTS SET
while ($line = mysql_fetch_assoc($result))
{
// GET EASY-TO-READ LOCAL VARIABLES
foreach ($line as $key => $val) { $$key = htmlentities($val); }
// CREATE THE ROW OF DATA
echo "<tr>";
echo "<td>>This table has pages me = clever</td>\n";
echo "</tr>\n";
} // END WHILE ITERATOR
echo "</table>\n";
} // END IF/ELSE
/****** build the pagination links ******/
// range of num links to show
$range = 2;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
} // end if
/****** end build pagination links ******/
?>Obviously you can take out my example table part and change with your own =D
ALSO: you can not of looked very hard on google, it took me 30min to create a pageination system from google..
Re: php pagination
here is a perfect working example .. with downloadable/customizable source code .
http://www.vipercreations.com/tutorials ... 9&act=view
awesome
http://www.vipercreations.com/tutorials ... 9&act=view
awesome
Re: php pagination
synical21:
sorry m8 I haven't refreshed this post .. I saw yours now .. kinda looks like the link I posted above .. ur right about not trying hard, it took me slightly more than 30 mins to find this one . Thank you
sorry m8 I haven't refreshed this post .. I saw yours now .. kinda looks like the link I posted above .. ur right about not trying hard, it took me slightly more than 30 mins to find this one . Thank you