Page 1 of 1

Interesting pagination setup. Need some advice.

Posted: Thu Apr 20, 2006 2:15 pm
by mattyboi
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:

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>";
}
?>
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?

Posted: Thu Apr 20, 2006 2:26 pm
by feyd
using an "else" with each of your if's controlling the pagination will tell you when the previous and next category buttons should be shown, respectively.

Posted: Thu Apr 20, 2006 2:29 pm
by mattyboi
That would work, but my problem is I have no idea of what the next and previous categorys are. I am only pulling the records in the table with the assigned ImgType. I could go in and hardcode the next and previous categorys for each category, but I was wondering if anyone had any other ideas that would be more practical.

Posted: Thu Apr 20, 2006 2:32 pm
by feyd
Your images have a category assigned to them yes? And these categories are normally displayed in some order right? You have all the information you need to determine the previous and next category from that.