Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating number of pages from number of entries

Status
Not open for further replies.

jollekox

Programmer
Feb 3, 2004
23
NO
I have 47 entries of text in an SQL database, in Perl, I want to print out the last 15 of these (this is simple, I manage to do this), but I also want to print out how many pages these 47 entries would "occupy", if each page displays 15 entries.

I could do something like this:
Code:
$entries = 47;

$pages = int($entries / 15);
$page = $entries / 15;

This renders unusable due to inaccuracy concerning floats to integers.
(And if the number og entries is smaller than 15, I will get a 0 from int(),
this can easely be fixed like this:
Code:
if ($pages == 0) {$pages = 1;}

But a number like 47 will return 3.13 when divided by 15, so I will have to increment it to 4 instead of 3.

How should I solve this problem? Thanks in advance!
 
Code:
$pages=$entries/$lines_per_page;
if ($pages > int($pages)) { $pages++; }

Is this what you're looking for
HTH
--Paul
 
I didn't quite get that one to work, but at this point, I am using this one:
Code:
$pages = $entries / 15;

unless ($pages !~ /./) {
$pages = int($pages);
$pages ++;
}

Thanks for your help!
 
$entries = 47;

@entriesPerPage = (15, 16, 23, 24, 1, 46, 47, 48);

foreach $entriesPerPage (@entriesPerPage) {
$pages = ($entries-1) / $entriesPerPage;
print "$entries entries @ $entriesPerPage entries/page would make ".int(++$pages)." page(s)\n";
}


Kind Regards
Duncan
 
I hope this helps
I have a prog which requires knowing the size of a file in order to fit on a 1.44M floppy. Surely you remember those - ha ha.
I write the information to a text file then get the size of the text file. The purists will say I am taking up valuable server time but it gets the job done and causes no problems to our system. Maybe an adaptation of this idea could solve your problem.
Keith
 
jollekox

The following seems to work OK, even when there are zero entries returned from the SQL call:
Code:
$entries = 47;
$page_size = 15;

$pages = int(($entries + $page_size - 1)/$page_size);

unless you want to argue that it takes 1 page to tell the user that there are no entries...

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top