Solved

Creating An image gallerywith multiple page results

Posted on 2006-11-08
13
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…

696 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