Re: PHP based Pagination
Posted: Tue Apr 27, 2010 4:16 pm
Wherever you call your function to display the page links, pass mysql_num_rows($result) as the function parameter.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
//Pagination Function Start
function navigate_images($total) {
global $section, $pg;
$pages = $total <= $section ? 1 : ceil($total / $section);
if(trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'pg='))
$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
else
$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
} else
$query = '?pg=';
$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
echo '<div class="navigate_images">';
echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
// this is to display a max of 9 links
$begin = $pg < 6 ? 1 : $pg - 4;
$end = $begin + 8;
while($end > $pages)
$end--;
for($i=$begin; $i<=$end; $i++)
echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
echo '</div>'."rn";
}
?>Code: Select all
function navigate_images($total) {
global $section, $pg;
$pages = $total <= $section ? 1 : ceil($total / $section);Code: Select all
// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));
// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
$lim_start = 15 * ($page - 1);
}
else {
$lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error());
/****************
DISPLAY YOUR IMAGES HOWEVER YOU WANT
****************/
// Show pagination links
navigation_images($total_images);
Code: Select all
<?php
include('includes/title.inc.php');
// include MySQL connector function
if (! @include('includes/connection.inc.php')) {
echo 'Sorry, database unavailable';
exit;
}
// create a connection to MySQL
$conn = dbConnect('query');
// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));
// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
$lim_start = 15 * ($page - 1);
}
else {
$lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error());
// extract the first record as an array
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TCTO | Image Gallery</title>
<link href="css/tcto.css" rel="stylesheet" type="text/css" />
<link href="css/tcto-gallery.css" rel="stylesheet" type="text/css" />
<LINK REL="BOOKMARK ICON" HREF="/favicon.ico">
</head>
<body>
<div id="container">
<div id="header">
</div>
<div class="style_linkprim" id="nav">
<ul class="class1">
<li>
<a href="index.php" target="_self">Home</a>
<a href="gallery.php" target="_self">Gallery</a>
<a href="upload.php">Upload Yourself</a>
<a href="contact.php" target="_self">Contact Me</a>
</li>
</ul>
</div>
<div id="article">
<h1 class="h1">Image Gallery<span class="p"><a name="back_to_top"></a></span></h1>
<p class="p">A variety of Chris's from a variety of places</p>
<div id="maincontent">
<div id="gallery">
<div class="p" id="main_image">
<!-- <a href="http://www.thechristakeover.com/portfolio/"><img src="images/tcto_me.jpg" width="400" height="300" border="0"></a>
<p class="p">Chris Wray, Doagh</p>-->
<?php
//Image call start
while($row = mysql_fetch_assoc($result)){
//get the name and caption for the main image
$mainImage = $row['filename'];
$caption = $row['caption'];
// get the dimensions of the main image
$imageSize = getimagesize('uploads/'.$mainImage);
echo "<p><img src='uploads/".$mainImage."' alt='".$caption."' ".$imageSize[3]." /></p>";
echo "<p>".$caption."</p>";
}
//Pagination Function Start
navigation_images($total_images);
function navigate_images($total) {
global $section, $pg;
$pages = $total <= $section ? 1 : ceil($total / $section);
$total = mysql_num_rows($result)
if(trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'pg='))
$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
else
$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
} else
$query = '?pg=';
$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
echo '<div class="navigate_images">';
echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
// this is to display a max of 9 links
$begin = $pg < 6 ? 1 : $pg - 4;
$end = $begin + 8;
while($end > $pages)
$end--;
for($i=$begin; $i<=$end; $i++)
echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
echo '</div>'."rn";
}
?>
</div>
</div>
<p class="p"><?php echo navigate_images($pg, $pages, $next, $last); ?></p>
</div>
<p class="p"><a href="#back_to_top" class="p">Back to top</a></p>
</div>
</body>
</html>Code: Select all
<?php
include('includes/title.inc.php');
// include MySQL connector function
if (! @include('includes/connection.inc.php')) {
echo 'Sorry, database unavailable';
exit;
}
// create a connection to MySQL
$conn = dbConnect('query');
// Get the total # of images
$query = "SELECT * FROM images";
$total_images = mysql_num_rows(mysql_query($query));
// Now do the pagination
$page = $_GET['pg'];
if($page > 0) {
$lim_start = 15 * ($page - 1);
}
else {
$lim_start = '0';
}
$sql = 'SELECT * FROM images LIMIT '.$lim_start.', 15';
// submit the query
$result = mysql_query($sql) or die (mysql_error());
// extract the first record as an array
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TCTO | Image Gallery</title>
<link href="css/tcto.css" rel="stylesheet" type="text/css" />
<link href="css/tcto-gallery.css" rel="stylesheet" type="text/css" />
<LINK REL="BOOKMARK ICON" HREF="/favicon.ico">
</head>
<body>
<div id="container">
<div id="header">
</div>
<div class="style_linkprim" id="nav">
<ul class="class1">
<li>
<a href="index.php" target="_self">Home</a>
<a href="gallery.php" target="_self">Gallery</a>
<a href="upload.php">Upload Yourself</a>
<a href="contact.php" target="_self">Contact Me</a>
</li>
</ul>
</div>
<div id="article">
<h1 class="h1">Image Gallery<span class="p"><a name="back_to_top"></a></span></h1>
<p class="p">A variety of Chris's from a variety of places</p>
<div id="maincontent">
<div id="gallery">
<div class="p" id="main_image">
<!-- <a href="http://www.thechristakeover.com/portfolio/"><img src="images/tcto_me.jpg" width="400" height="300" border="0"></a>
<p class="p">Chris Wray, Doagh</p>-->
<?php
function navigate_images($total) {
global $section, $pg;
$pages = $total <= $section ? 1 : ceil($total / $section);
//Image call start
while($row = mysql_fetch_assoc($result)){
//get the name and caption for the main image
$mainImage = $row['filename'];
$caption = $row['caption'];
// get the dimensions of the main image
$imageSize = getimagesize('uploads/'.$mainImage);
echo "<p><img src='uploads/".$mainImage."' alt='".$caption."' ".$imageSize[3]." /></p>";
echo "<p>".$caption."</p>";
}
//Pagination Function Start
if(trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'pg='))
$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
else
$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
} else
$query = '?pg=';
$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
echo '<div class="navigate_images">';
echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
// this is to display a max of 9 links
$begin = $pg < 6 ? 1 : $pg - 4;
$end = $begin + 8;
while($end > $pages)
$end--;
for($i=$begin; $i<=$end; $i++)
echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
echo '</div>'."rn";
}
?>
</div>
</div>
<p class="p"><?php echo navigate_images($pg, $pages, $next, $last); ?></p>
</div>
<p class="p"><a href="#back_to_top" class="p">Back to top</a></p>
</div>
</body>
</html>Code: Select all
function navigate_images($total) {
global $section, $pg;
$pages = $total <= $section ? 1 : ceil($total / $section);
if(trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'pg='))
$query = '?'.preg_replace('/pg=d+/', 'pg=', $_SERVER['QUERY_STRING']);
else
$query = '?'.$_SERVER['QUERY_STRING'].'&pg=';
} else
$query = '?pg=';
$first = '<a href="'.$_SERVER['PHP_SELF'].$query.'1">First Page</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">Previous</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">Next</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">Last Page</a>';
echo '<div class="navigate_images">';
echo $pg > 1 ? "$first | $prev |" : 'First Page | Previous |';
// this is to display a max of 9 links
$begin = $pg < 6 ? 1 : $pg - 4;
$end = $begin + 8;
while($end > $pages)
$end--;
for($i=$begin; $i<=$end; $i++)
echo $i == $pg ? ' ['.$i.'] ' : ' <a href="'.$_SERVER['PHP_SELF'].$query.$i.'">'.$i.'</a> ';
echo $pg < $pages ? "| $next | $last" : '| Next | Last Page';
echo '</div>'."rn";
}