View Full Version : page calculation
linuxis
06-08-2006, 10:46 AM
On my site I display a given number of images ($imp) on each page. I calculate how many pages there are with this formula :
$pages=int($count / $imp)+1;
# $imp :: number of images per page (for example 16)
# $count :: total number of images (for example 50)
This seems to work fine, but I've got a problem though.
When I use:
$count=20;
$imp=20;
The calculation says there are 2 pages, while there should be 1
same with:
$count=40;
$imp=20;
Calculation says there are 3 pages, while there should be 2
I know what the problem is, but I dont know how to solve this :confused:
Who helps me ? Thanks in advance
Aradon
06-08-2006, 02:24 PM
On my site I display a given number of images ($imp) on each page. I calculate how many pages there are with this formula :
$pages=int($count / $imp)+1;
# $imp :: number of images per page (for example 16)
# $count :: total number of images (for example 50)
This seems to work fine, but I've got a problem though.
When I use:
$count=20;
$imp=20;
The calculation says there are 2 pages, while there should be 1
same with:
$count=40;
$imp=20;
Calculation says there are 3 pages, while there should be 2
I know what the problem is, but I dont know how to solve this :confused:
Who helps me ? Thanks in advance
$pages=int($count / $imp)+1;
I will bold the section that is giving you your results..
$pages=int($count / $imp)+1;
So if we do (20 / 20) + 1 we get 2
(40 / 20 ) + 1 = 3 !
So I guess the question is, why are you doing the + 1 ?
linuxis
06-08-2006, 02:38 PM
Well I added it cause something like:
$count="10";
$imp="20";
gives as result: 0 (which should be 1)
Aradon
06-08-2006, 05:57 PM
Well I added it cause something like:
$count="10";
$imp="20";
gives as result: 0 (which should be 1)
While this may not answer your question completely, how can imp (being the number of images per page) be larger then the count (which is the total number of images)
Or did you mean total number of unique images .. ?
FishMonger
06-08-2006, 06:09 PM
$pages = $count % $imp ? int($count / $imp)+1 : int($count / $imp);
FishMonger
06-08-2006, 06:15 PM
You may want to take a look at some of the paging modules.
use Data::Pageset;
http://search.cpan.org/~llap/Data-Pageset-1.03/lib/Data/Pageset.pm
use Data::Page::Navigation;
http://search.cpan.org/~kazeburo/Data-Page-Navigation-0.03/lib/Data/Page/Navigation.pm
Search through this list for others.
http://search.cpan.org/search?query=page&mode=all
linuxis
06-08-2006, 06:19 PM
thanks fishmonger, that code seems to work.. ill check out the links :)
aradon tnx..
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.