Solved

Creating An image gallerywith multiple page results

Posted on 2006-11-08
13
170 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 41
Inserting results of While loop to sql server 5 38
PHP Web Development 6 28
Do not understand error message 3 26
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

726 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