Creating An image gallerywith multiple page results

I am fimiliar with php and mysql but am wondering how can i create an image gallery where i can have images 1-10 on one page then have images 11 -20 on the next etc.. until there are no more images.
LVL 4
John-BaylesAsked:
Who is Participating?
 
VoteyDiscipleCommented:
Yes, but then you'll have to change the numbering scheme on all your links too.  They'd have to look like...
<a href="whatever.php?page=3">Page 4</a>

... and that's just confusing, and will eventually drive you crazy.

Stick with 1-based numbering for $_GET['page'] and just subtract 1 in the formula.
0
 
VoteyDiscipleCommented:
It's surprisingly easy.  You'll do something like:


$perPage = 10; // How many images to display on each page?

// Assume we're on page 1 if the URL doesn't say otherwise:
$page = ($_GET['page']) ? ($_GET['page']) : 1;

// What's the first image that should get displayed on that page?
$start = $page * $perPage + 1;

$query = 'SELECT SQL_CALC_FOUND_ROWS *'
. ' FROM images'
. ' LIMIT ' . $start . ', ' . $perPage
;
// Run this query to get the list of images

$query = 'SELECT FOUND_ROWS()';
// Run this query to figure out how many total images WOULD HAVE GOTTEN selected by the previous query if there weren't a limit.


From that you can figure out how many pages there are (total number of images / images per page) and draw any page navigation you like.  What goes in the page navigation?  Just pass in page=3 for the third page, page=4 for the fourth, et cetera.  That's all there is to it.
0
 
John-BaylesAuthor Commented:
Could you explain about how to tell it what images to load first on each page
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
VoteyDiscipleCommented:
What do you mean by "what images to load first on each page"?  What order the images go in?

That would be determined by the ORDER BY clause in your query.

Have I completely misinterpreted that?
0
 
John-BaylesAuthor Commented:
i described it preety badly! like as in how dodies it know what image to load on the following page
Database:
Filename         URL
test.jpg     full/test.jpg
test2.jpg     full/test2.jpg
test3.jpg     full/test3.jpg
test4.jpg     full/test4.jpg

lets just say it wants two images per page how does it know to load image3.jpg on the second page? and not image1.jpg again
0
 
VoteyDiscipleCommented:
That's just how the LIMIT clause works.  You can imagine it runs the entire query, gets all four of those results, but then you've told it to LIMIT the results to only two rows starting at row 2 -- that's test3.jpg and test4.jpg

Give it a try at a MySQL prompt if you want to get a better feel for how it works, or take a look at the documentation in the MySQL manual ( http://dev.mysql.com/doc/refman/5.0/en/select.html )

The way I wrote the query above (though I omitted an ORDER BY clause, which you should really have so the results will be consistent) it will return $perPage images (e.g. 10 images) starting at row number $start.
0
 
John-BaylesAuthor Commented:
sorry to be annoying but if

. ' LIMIT ' . $start . ', ' . $perPage

where start is the record to start from and per page is the number to get then wouldnt the first end up like

$perPage = 10; /
$page = ($_GET['page']) ? ($_GET['page']) : 1;
$start = $page * $perPage + 1;
there for:
$start = 1 * 10 + 1
Start = 11

. ' LIMIT ' 11 ', ' 10
???
0
 
VoteyDiscipleCommented:
Heh, good point.  It'd probably help to do ($page - 1) * $perPage + 1

That's certainly how I envisioned it working when I checked the math in my head... I'm just still waiting for what goes on in my head to become a viable way of writing programs.
0
 
John-BaylesAuthor Commented:
hahahahaha :P do you reckon this would work?

$perPage = 10; /
$page = ($_GET['page']) ? ($_GET['page']) : 0;
$start = $page * $perPage + 1;
there for:
$start = 0 * 10 + 1
Start = 1

. ' LIMIT ' 1 ', ' 10

0
 
John-BaylesAuthor Commented:
actually that probably wont work beacuase then page will be 0 so the next page will be page 1 not page 2
0
 
ajaikkumarCommented:
see simple concept is break your incoming result ufing limit in mysql query
not if you are on
   page 0 the limit shoud be LIMIT 1,10
if page 1 the limit shoud be LIMIT 11,20 and so on

so u need start and end  for LIMIT on the basis of page number.




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.