Solved

Creating An image gallerywith multiple page results

Posted on 2006-11-08
13
168 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:John-Bayles
  • 5
  • 5
13 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17903074
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
 
LVL 4

Author Comment

by:John-Bayles
ID: 17903213
Could you explain about how to tell it what images to load first on each page
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17903240
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 4

Author Comment

by:John-Bayles
ID: 17903287
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
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17903312
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
 
LVL 4

Author Comment

by:John-Bayles
ID: 17903566
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
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17903582
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
 
LVL 4

Author Comment

by:John-Bayles
ID: 17903594
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
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 63 total points
ID: 17903612
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
 
LVL 4

Author Comment

by:John-Bayles
ID: 17903614
actually that probably wont work beacuase then page will be 0 so the next page will be page 1 not page 2
0
 
LVL 2

Assisted Solution

by:ajaikkumar
ajaikkumar earned 62 total points
ID: 17905471
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question