tags:

views:

352

answers:

3

Anyone Please help me in implementing Paging in my project. I have nearly hundred pictures in images folder. When the user clicks the gallery link, the page will be directed to gallery.php, where the pictures should be shown 10 by 10. Please help me i need solution soon.

A: 

If you're loading the pictures from a folder, it may be best to simply load the file names in as array-entries, and then show only 10 items from the array at a time, tracking your "page" (really your array start-index) via a $_GET variable in the address bar.

Jonathan Sampson
Thank You for your support
Rajasekar
+1  A: 

You could pass a GET parameter through the URL like so:

http://yourserver.com/gallery.php?p=1

Where variable p represents the page number.

Then, inside your php script, have it calculate where to start looking inside the database. If you say you need to display 10 by 10, I am assuming 100 pictures per page. So

$limit = 100;
$start = $_GET['p'] * $limit - $limit;

Then your SQL query would be to select $limit entries from $start:

$sql = "SELECT * FROM `yourtphototable` LIMIT {$start},{$limit};";

and then have an html link to the naxt page:

$nextpage = $_GET['p'] + 1;
$link = '<a href="http://yourserver.com/gallery.php?p=' . $nextpage . '">Next</a>';
Yuri
It is very helpful - Thanks
Rajasekar
Yuri. I think you mean $start = $_GET['p'] * $limit - $limit; Otherwise page 1 starts at 100.
hobodave
Edited to fix starting at wrong limit.
hobodave
I'd do ($_GET['p']-1)*$limit. And if($start<0) $start = 0; And other checks to make sure the user isn't inserting something stupid into your code. I also find $limit deceptive.... I'd prefer $pics_per_page or something.
Mark
whether i have to store pictures in database or folder
Rajasekar
A: 

If you are using MYSQL, as the tag suggests, then you can pass the page they are on in the URL as Jonathan suggested, and GET is nice as it enables the user to jump to where they want to be, you can bookmark a favorite page, etc, and on the mysql side just make certain that you are then use the LIMIT command.

You can find some help on this page, btw: http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

James Black