Interesting pagination setup. Need some advice.
Posted: Thu Apr 20, 2006 2:15 pm
Hey guys,
I am using a really simple pagination script to scroll through my photo gallery. You can view the gallery at: http://www.phoenixmason.com and then click gallery.
As you can see when you select a category on the left hand side you can scroll through all the pictures in that category by using the next and previous buttons.
The code is pretty simple. When you choose a category it passes a query string with the appropriate ImgType to the next page. It then selects all the records in the table with that ImgType starting with the first record. See the code below:
The previous and next buttons will disappearing according to the first or last record. It works great but I would like to switch it up a bit and I am kind of lost.
When you come to the last record; instead of the next button disappearing I would like it to be replaced by the next category button. And the same for the first record being replaced by the previous category button.
Any Ideas?
I am using a really simple pagination script to scroll through my photo gallery. You can view the gallery at: http://www.phoenixmason.com and then click gallery.
As you can see when you select a category on the left hand side you can scroll through all the pictures in that category by using the next and previous buttons.
The code is pretty simple. When you choose a category it passes a query string with the appropriate ImgType to the next page. It then selects all the records in the table with that ImgType starting with the first record. See the code below:
Code: Select all
<?php
include 'includes/dblink.php';
$imgtype = preg_replace('/[^a-zA-Z0-9]/', '', $_GET['imgtype']);
$cnt = is_numeric( $_GET['cnt'] )?$_GET['cnt']:0;
if (!isset($cnt)) {
$cnt = 0;
}
if (!isset($cnt2)) {
$cnt2 = 1;
}
//select proper row in table based on cnt.
$sql = "SELECT * FROM `images` WHERE `ImgType` = '$imgtype' and `ImgSwitch` = 1";
$sql .= " LIMIT $cnt, 1";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$imgid = $row['ImgId'];
$imgname = $row['ImgName'];
//calculate how many rows in table to set prev/next buttons.
$sql2 = "SELECT * FROM `images` WHERE `ImgType` = '$imgtype' and `ImgSwitch` = 1";
$result2 = mysql_query($sql2);
$total_records = mysql_num_rows($result2);
//subtract one from total b/c starts with zero.
$total_records--;
if ($cnt > 0) {
$prev_cnt = $cnt - 1;
$prev_cnt2 = $cnt2 - 1;
$url = "gal2.php?cnt=" . $prev_cnt . "&imgtype=" . $imgtype . "&cnt2=" . $prev_cnt2;
$previous = "<a href=\"#\" onclick=\"location='$url'\"><img src='images/galbuttons/previous.gif' border='0' alt='Previous'></a>";
}
if ($cnt < $total_records) {
$next_cnt = $cnt + 1;
$next_cnt2 = $cnt2 + 1;
$url = "gal2.php?cnt=" . $next_cnt . "&imgtype=" . $imgtype . "&cnt2=" . $next_cnt2;
$next = "<a href=\"#\" onclick=\"location='$url'\"><img src='images/galbuttons/next.gif' border='0' alt='Next'></a>";
}
?>When you come to the last record; instead of the next button disappearing I would like it to be replaced by the next category button. And the same for the first record being replaced by the previous category button.
Any Ideas?