Solved

Creating An image gallerywith multiple page results

Posted on 2006-11-08
13
166 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
Comment Utility
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
Comment Utility
Could you explain about how to tell it what images to load first on each page
0
 
LVL 19

Expert Comment

by:VoteyDisciple
Comment Utility
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
 
LVL 4

Author Comment

by:John-Bayles
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Author Comment

by:John-Bayles
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now