CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Another way besides using ceil?

 



hwnd
User

Mar 31, 2013, 1:13 PM

Post #1 of 7 (673 views)
Another way besides using ceil? Can't Post

I am trying to return the current page number and total number of pages without using ceil. My web server will not allow installs of certain modules such as Data::Page. So would this pass for a way of doing this?


Code
  

my $page_id = param('id');
$page_id = 1 if !defined ( $page_id ) || $page_id !~ /^\d+$/ || $page_id < 1;

my $per_page = param('use');
$per_page = 10 if !defined ( $per_page ) || $per_page !~ /^\d+$/ || $per_page < 1;


my ($pg_curr, $pg_total) = $dbh->page_count("tablename", $page_id, $per_page);


print h1("Page $pg_curr (of ", $pg_total, ')');


# sub routine in module

sub page_count {
my ($self, $tbl, $pg_id, $per_page) = @_;
my $dbh = $self->dbh();

my $sth = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM $tbl");

my $pg_curr = int($pg_id / $per_page)+1;
my $pg_total = int($sth->[0] / $per_page)+1;

return ($pg_curr, $pg_total);
}



Laurent_R
Veteran / Moderator

Mar 31, 2013, 1:57 PM

Post #2 of 7 (666 views)
Re: [hwnd] Another way besides using ceil? [In reply to] Can't Post

This code:


Code
my $pg_curr  = int($pg_id / $per_page)+1;


is probably wrong if $pg_id is a round multiple of $per_page.

Suppose $page_id is 150 and $per_page 50, you probably want to set $pg_curr to 3, but your code will assign 4 to $pg_curr.

Same thing for $pg_total.


Chris Charley
User

Mar 31, 2013, 2:28 PM

Post #3 of 7 (655 views)
Re: [hwnd] Another way besides using ceil? [In reply to] Can't Post

You could use POSIX which has the function ceil.

POSIX was first released with perl 5


Zhris
Enthusiast

Mar 31, 2013, 2:59 PM

Post #4 of 7 (645 views)
Re: [Chris Charley] Another way besides using ceil? [In reply to] Can't Post

Or since the OP explicitly mentioned "without using ceil" as part of his requirements:


Code
print round_up(4) . "\n"; 
print round_up(4.1) . "\n";

sub round_up
{
return $_[0] == int $_[0] ? $_[0] : int $_[0] + 1;
}


Chris


Zhris
Enthusiast

Mar 31, 2013, 3:25 PM

Post #5 of 7 (639 views)
Re: [hwnd] Another way besides using ceil? [In reply to] Can't Post

I think I'm probably missing something here, but it appears you are calculating the current page number ( $pg_curr). I thought $page_id was the current page number (assumed supplied by the user).

I would expect a basic pagination system to expect the arguments:


Code
current_page     ( 4  ) ( user supplied          ) 
results_per_page ( 10 ) ( possibly user supplied )
total_results ( 34 ) ( calculated )


Then return "indexes" to the first and last result for the purpose of later retrieving the data AND the last page for the purpose of later constructing the page links:


Code
first_result ( 31 ) 
last_result ( 34 )
last_page ( 4 )


Have I misunderstood something.

Chris


(This post was edited by Zhris on Mar 31, 2013, 3:32 PM)


hwnd
User

Mar 31, 2013, 3:43 PM

Post #6 of 7 (630 views)
Re: [Laurent_R] Another way besides using ceil? [In reply to] Can't Post

Ok let's see. I'll break this down a bit.


Code
   

# Page we are on ( e.g. ?id=1;use=10 )

my $page_id = param('id') || 1;



# Records we want to use per page (default 10)

my $per_page = param('use') || 10;



# Calling the sub routine from package module

my ($curr, $total) = $dbh->page_count("dbtablename", $page_id, $per_page);



# sub routine in package module

sub page_count {

my ($self, $tbl, $pg_id, $per_page) = @_;

my $dbh = $self->dbh();

my $sth = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM $tbl");

my $curr = int($pg_id / $per_page) + 1;

my $total = int($sth->[0] / $per_page) + 1;

return ($curr, $total);

}



Say we have 40 records total, it indexes through the script showing results (e.g ?id=1;use=10, ?id=11;use=10, ?id=21;use=10, and so on. )

So for how I am doing this if I put ?id=1;use=40 it indexes page (1 of 2) with only being one page


(This post was edited by hwnd on Mar 31, 2013, 3:45 PM)


hwnd
User

Apr 1, 2013, 3:57 PM

Post #7 of 7 (598 views)
Re: [hwnd] Another way besides using ceil? [In reply to] Can't Post

This seems to work if I use a modulos operator.


Code
   

my $pg_curr = int($pg_id / $per_page)+1;

my $pg_total = int($sth->[0] / $per_page);

++$pg_total if ($sth->[0] % $per_page);

Or this would work also:

Code
 
my $total_recs = $dbh->selectrow_array("SELECT COUNT(*) FROM table");



my $count = 0;

for (my $first = 1; $first <= $total_recs; $first += $per_page)

{

$count++;

my $last = $first + $per_page - 1;

$last = $total_recs if $last > $total_recs;



my $label = $count;

my $link;



if ($first != $start) {

my $url = sprintf ("%s?start=%d;per_page=%d",url(),$first,$per_page);

$link = a ({-href => $url}, $label);

} else {

$link = $label;

}

print "[$link] ";

}



(This post was edited by hwnd on Apr 1, 2013, 10:07 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives