You are here: Re: "next" link question « PHP Language « IT news, forums, messages
Re: "next" link question

Posted by Shawn Wilson on 11/23/05 17:36

I have a photo album site that I do this on. What I do hits the database on
each page load, but it works quite well for me.

The concept is just using the LIMIT function of SQL to only request one (or
more) records from the set your query finds.

Here is one of my SQL statements:

SELECT albumid FROM albums ORDER BY timestamp DESC LIMIT $start_album,
$num_albums

$start_album signifies what record to start at, and $num_albums is the total
results to return. On my site, I'm showing 15 thumbnails at a time so a
real example would be:

SELECT albumid FROM albums ORDER BY timestamp DESC LIMIT 45, 15

That starts at record 45 and returns 15 results.

I use the same logic for single picture displays... only it ends up
something like this:

SELECT picid FROM pics ORDER BY timestamp DESC LIMIT 12, 1

That gets the info for the 12th picture in the series (starts at zero
remember) and only returns that one result.

You can see it live here if you like: http://www.dvigroup.net/album/

A page example of the URL is here:
http://www.dvigroup.net/album/set.php?setid=284&setpage=2

In my build_page_stuff function, I validate the page number sent - which is
pretty easy. People can type in whatever they want up there and can't mess
it up. Try to break it... (if you succeed, let me know please, but I'm
pretty sure it's bulletproof). At worst, they get put on page 1. I do that
with intval(). That simply turns any text input into a zero effectively.
Then a quick less than zero check and a max number check makes sure that
number in in the right range.

Good luck.


This is the complete function that I use to make my previous and next links:
---------------
function
build_page_stuff_array($page,$pic_count,$album_count,$sourceref,$albumid) {
if ($sourceref == "album") { $extraurl = "albumid=$albumid&"; }

# check for valid page var
$page = intval($page);
if ($page < 1) { $page = 1; }
$max_page = intval($album_count / $pic_count) + 1;
if ($page > $max_page) { $page = $max_page; }

# make pre and next links
$page_pre = $page - 1;
if ($page_pre < 1) { $page_pre = 0; }
$page_next = $page + 1;
if ($page_next > $max_page) { $page_next = 0; }

if ($page_pre > 0) { $page_pre_link =
<<<END
<a href="$sourceref.php?{$extraurl}page=$page_pre"><img id="img_pre"
src="gfx/prev.gif" alt="prev" /></a>
END;
}
else { $page_pre_link =
<<<END
<img id="img_pre" src="gfx/prev_gray.gif" alt="prev" />
END;
}
if ($page_next > 1) { $page_next_link =
<<<END
<a href="$sourceref.php?{$extraurl}page=$page_next"><img id="img_next"
src="gfx/next.gif" alt="next" /></a>
END;
}
else { $page_next_link =
<<<END
<img id="img_next" src="gfx/next_gray.gif" alt="next" />
END;
}

$array_page_stuff = array('page' => $page, 'max_page' => $max_page,
'page_pre' => $page_pre, 'page_next' => $page_next, 'page_pre_link' =>
$page_pre_link, 'page_next_link' => $page_next_link);

return $array_page_stuff;
}
---------------

Here is the code that calls it:

$pic_count = 15;
$album_count = get_album_count();
$sourceref = "index";
$array_page_stuff =
build_page_stuff_array($page,$pic_count,$album_count,$sourceref,$albumid);


For a single image page:

$pic_count = 1;
$album_count = get_pic_count($albumid);
$sourceref = "detail";
$array_page_stuff =
build_page_stuff_array($page,$pic_count,$album_count,$sourceref,$albumid);

--
Shawn Wilson

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация