PHP based Pagination
Posted: Tue Apr 27, 2010 5:50 am
hey,
I've created a gallery page that displays images stored in a database dynamically using PHP. That all works fine but now as the site gets bigger I want to try split the images across several pages so as anyone on the site does'nt spend their entire time scrolling. Ideally I'd like 15 or so images per page but the code I'm using won't integrate too well with what I've already done as I'm new to this and not entirely sure what I'm doing.
Code In Header -
Code In Body -
The page itself can be seen at http://www.thechristakeover.com/gallery.php
The links for the pagination appear at the bottom of the page but don't do anything in regards to navigating through anything. They just refresh the gallery. Any help would be fantastic!!
Chris
I've created a gallery page that displays images stored in a database dynamically using PHP. That all works fine but now as the site gets bigger I want to try split the images across several pages so as anyone on the site does'nt spend their entire time scrolling. Ideally I'd like 15 or so images per page but the code I'm using won't integrate too well with what I've already done as I'm new to this and not entirely sure what I'm doing.
Code In Header -
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');
// prepare SQL to retrieve image details
$sql = 'SELECT * FROM images';
// suvmit the query
$result = mysql_query($sql) or die (mysql_error());
// extract the first record as an array
?>
Code: Select all
<?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
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">«</a>';
$prev = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg - 1).'">‹</a>';
$next = '<a href="'.$_SERVER['PHP_SELF'].$query.($pg + 1).'">›</a>';
$last = '<a href="'.$_SERVER['PHP_SELF'].$query.$pages.'">»</a>';
echo '<div class="navigate_images">';
echo $pg > 1 ? "$first : $prev :" : '« : ‹ :';
// 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" : ': › : »';
echo '</div>'."rn";
}
?>
The links for the pagination appear at the bottom of the page but don't do anything in regards to navigating through anything. They just refresh the gallery. Any help would be fantastic!!
Chris