开发者

Create pagination as well as number per page selection on PHP image gallery

I've got a simple PHP image gallery where the PHP reads data from a MySQL database and organizes and links each one to a larger version. It works well but places too many images on the page.

I'm hoping someone can show me how to first split the images into multiple pages, as well as create navigation for users through the pages.

If it's possible, I would also like the user to be able select how many images should be displayed per page, with a default of, say, 16.

Below is my current gallery's code:

<?php
$mysql_link = mysql_connect("localhost", "root", "root");   
mysql_select_db("new_arrivals_imgs") or die("Could not select database");

$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
                     "ORDER BY `imgDate` DESC") or die(mysql_error());

if(!$query) {
 echo "Cannot retrieve information from database.";
} else {
 while($row = mysql_fetch_assoc($query)) {
  echo "<li><a href='new_arrivals_img/".$row['imgURL'].
           "' class='gallery' title='".$row['imgTitle'].
           "'><img src='new_arrivals_img/thum开发者_JAVA技巧bnails/".
           $row['imgURL']."'></a></li>";
 }
}
?>

Thanks in advance for your help.


You could add LIMIT to the end of your query string.

$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());

The first part of limit is where to start and the second is the length.

$limit can be set by a select box or user text and $curPage can be carried through url parameters.

http://dev.mysql.com/doc/refman/5.1/en/select.html


If you wind up with many pages (i.e. 100+) you might want to look at "logarithmic" page navigation, as described here:

How to do page navigation for many, many pages? Logarithmic page navigation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜