Restricting the number of list items? [closed]
What I'm aiming for is a limit on the number of list items on one page, and then for them to be transfered onto a new page. I want to do this so I can add new list items quickly and without worrying if the page is too long/takes too long to load. A point in the right direction would be great, and an example of the code I need would be better! Thank you
The general technique is called paging. See, for example, http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx
The idea is to use a LIMIT
clause in your SQL database query to retrieve a subset of results, specified by an offset within the list of records and the number of records starting at that offset to retrieve. You then modify gallery.php
to accept a page
GET parameter (as the tutorial that I linked to does; i.e. gallery.php?page=3
) or an offset
GET parameter (i.e. gallery.php?offset=36
) and modify the LIMIT
clause appropriately. For example, if you have 12 items to a page, then page 2 of the gallery displays records 12 through 23 inclusive (counting from zero).
EDIT: Here is example PHP code:
<ul class="ulgallery">
<?php
$gallery_items = array(
array("img_src" => "image1.png", "title" => "This is image 1.", "author" => "John Doe"),
array("img_src" => "image2.png", "title" => "This is image 2.", "author" => "John Doe"),
array("img_src" => "image3.png", "title" => "This is image 3.", "author" => "John Doe"),
array("img_src" => "image4.png", "title" => "This is image 4.", "author" => "John Doe"),
array("img_src" => "image5.png", "title" => "This is image 5.", "author" => "John Doe"),
array("img_src" => "image6.png", "title" => "This is image 6.", "author" => "John Doe"),
array("img_src" => "image7.png", "title" => "This is image 7.", "author" => "John Doe"),
array("img_src" => "image8.png", "title" => "This is image 8.", "author" => "John Doe"),
array("img_src" => "image9.png", "title" => "This is image 9.", "author" => "John Doe"),
array("img_src" => "image10.png", "title" => "This is image 10.", "author" => "John Doe"),
array("img_src" => "image11.png", "title" => "This is image 11.", "author" => "John Doe"),
array("img_src" => "image12.png", "title" => "This is image 12.", "author" => "John Doe"),
array("img_src" => "image13.png", "title" => "This is image 13.", "author" => "John Doe"),
// ...
array("img_src" => "image304.png", "title" => "This is image 304.", "author" => "John Doe")
);
define("ITEMS_PER_PAGE", 12);
$page = isset($_GET["page"]) ? (int)$_GET["page"] : 0;
if ($page < 0)
$page = 0;
$num_pages = (int)((count($gallery_items) + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE);
if ($page > $num_pages)
$page = $num_pages;
$end_index = ($page + 1) * ITEMS_PER_PAGE;
if ($end_index > count($gallery_items))
$end_index = count($gallery_items);
for ($offset = $page * ITEMS_PER_PAGE; $offset < $end_index; ++$offset) {
$current_gallery_item = $gallery_items[$offset];
// output the <img> tag for $gallery_items[$offset];
echo '<li><a href="#"><img src="' . $current_gallery_item["img_src"] . '" /></a></li>';
}
?>
</ul>
I haven't tested it, but it is along the lines of what I am suggesting.
精彩评论