PDA

View Full Version : another php/mysql question


Dubird
February 7th, 2008, 04:39 PM
Ok, here's the problem. On my graphics site, I have a TON of different things. I want to be able to display say 5 of them, but on the bottom, I want to generate a page number list (like page 1 2 3 with links). I can't seem to word that question in a way that I can find it through searching, so I thought I'd ask here. I've seen it before and it was pretty easy, but now I can't find it. ^_^;;

Mathias
February 7th, 2008, 07:13 PM
Ok, here's the problem. On my graphics site, I have a TON of different things. I want to be able to display say 5 of them, but on the bottom, I want to generate a page number list (like page 1 2 3 with links). I can't seem to word that question in a way that I can find it through searching, so I thought I'd ask here. I've seen it before and it was pretty easy, but now I can't find it. ^_^;;

If the images have links in a database then use the limit option in your SQL. Your links at the bottom of the page pass the page number via a form or links with hard-coded $_GET parameters.

select * from `table` where some_condition order by some_variable desc limit page_number*limit, limit

select * from `table` where some_condition order by some_variable desc limit 25, 5

Dubird
February 8th, 2008, 03:58 PM
how does that create the page numbers and links to them at the bottom?

Mathias
February 8th, 2008, 08:35 PM
Count the number of images via sql query or by reading the directory. Divide the count by the number of images per page. Now that you know how many pages there are, create a link for each page passing the page number selected. Select the number of images from your limit starting at the page number times the limit using the limit sql example, or start a for loop with a starting position of the page number times the limit. Display your images.

select count(some_variable) as total_images from `table` where some_conditionUse the previous limit sql example to display your images.

If you are reading the directory directly then use the readdir function to read each filename into an array.

http://us3.php.net/manual/en/function.readdir.php

Use the opendir function to allow you to read the directory.

for ($i = $page_number * $limit; $i < $page_number * $limit + $limit; $i++)
{
// Create array of displayed images with the readdir function.
}Create the page number links in a list and style with CSS or print one after the other in a <div> or <p>.

print "<ol>";

for ($i = 0; $i < $total_images; $i++)
{
print "<li><a href=\"some_page.php?page_number=" . $i . "\">" . $i . "</a></li>";
}

print "</ol>";