PDA

View Full Version : Interesting pagination setup. Need some advice.


mattyboi
04-20-2006, 05:44 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:


<?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?:cool:

cdwhalley.com
04-20-2006, 07:51 PM
How about changing this bit:

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

to look like this:

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>";
} else {
//previous category link
}
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>";
} else {
//next category link
}

mattyboi
04-20-2006, 08:00 PM
My problem is I have no idea of what the next and previous categories would be since I am only grabing the records in the table with the assigned ImgType. I know I could hardcode the next and previous ImgTypes in but that would be a pain if I changed anything in the future. I wanted to keep this as dynamic as possible. Maybe I could use a CASE select but not quite sure.

Any other ideas?

Thanks

devinemke
04-20-2006, 08:45 PM
My problem is I have no idea of what the next and previous categories would be since I am only grabing the records in the table with the assigned ImgType.
store the list of categories in an array in the specific order you want them displayed. then when you are on the last image in the set use the next category in the array.

as a side note: the code you using in very ineffecient in that it grabs all of the data for each category on each page request. your code does this just to caluculate the $total_records variable. the total record count can be grabbed using a simple COUNT() query without having to get all of the data.

mattyboi
04-20-2006, 09:31 PM
Great advice! Since I am new to php could you post a small example of how to create an array listing the category order.

Also, how would I set the count() function up?

Thanks

devinemke
04-20-2006, 09:51 PM
Since I am new to php could you post a small example of how to create an array listing the category order.

$categories = array(
'Flagstone Patios',
'Brick Patios',
'Stamped Concrete Patios',

etc...

);

Also, how would I set the count() function up?

$result = mysql_query("SELECT COUNT(*) FROM images WHERE ImgType = '$imgtype' AND ImgSwitch = 1");
$total_records = mysql_result($result, 0);

mattyboi
04-21-2006, 07:11 PM
Thanks a lot!

How would I use the array though? Once I have created the order of the categories in the array, how would I tell it to go to the next category?

Arrays have always confused me.:confused:

devinemke
04-21-2006, 07:35 PM
$categories = array(
'Flagstone Patios',
'Brick Patios',
'Stamped Concrete Patios'
);

$current_category = 'Brick Patios'; // for example
$current_key = array_search($current_category, $categories);

if ($current_key == count($categories) - 1) {$next_key = 0;}
else {$next_key = $current_key + 1;}

$next_category = $categories[$next_key];
echo $next_category;

mattyboi
04-21-2006, 07:59 PM
Sweet, is there a way to make it so that when you on the first or last category it knows there is no previous or next category?

Right now when you are on the first category, the previous category would be the last category in the list.

devinemke
04-21-2006, 08:09 PM
$categories = array(
'Flagstone Patios',
'Brick Patios',
'Stamped Concrete Patios'
);

$current_category = 'Brick Patios';
$current_key = array_search($current_category, $categories);

if ($current_key == count($categories) - 1) {$next_key = false;}
else {$next_key = $current_key + 1;}

if ($next_key)
{
$next_category = $categories[$next_key];
echo $next_category;
}
else
{
echo 'no more categories';
}