in reply to Re: displaying 10 results per page
in thread displaying 10 results per page

Oh thank you! Yeah that seems to have solved the error issue. but my code still doesn't display the next ten profiles when I click on the next link. I tried printing out the $page value and that remains 1 even though the page url changes to http://blah blah/scriptname.cgi?page=2 when i click next once. It remains at page=2 on the url even when i click next for the third time. Is there something wrong with my logic?

Replies are listed 'Best First'.
Re^3: displaying 10 results per page
by ww (Archbishop) on May 26, 2012 at 11:32 UTC
    just a minor flaw:
    002: my $page = 1;
    and
    007:    my $start = ($page - 1) * 10;
    #!/usr/bin/perl use 5.014; # 972560 my $page = 1; my $start = ($page - 1) * 10; # please recite the rules on # evaluation order and the zero-times +table my $end = $start + 10; say "\$start: $start; \$end: $end";

    Output:

    $start: 0; $end: 10
Re^3: displaying 10 results per page
by Anonymous Monk on May 26, 2012 at 08:36 UTC

    Is there something wrong with my logic?

    What do you think this does  my $page = 1; ?

Re^3: displaying 10 results per page
by poj (Abbot) on May 27, 2012 at 14:19 UTC

    I'm not sure what the while loop is for if you only want to show one page. Here is slightly refactored code with it removed.

    sub browse_ten { # work out how many users my @users = glob("users_dir/*"); my $no_of_users = scalar(@users); # calculate how many pages to display my $page_last = ceil($no_of_users / 10) ; # get page to show from param or default 1 my $page_show = param('page') || 1; my $page_prev = $page_show - 1; my $page_next = $page_show + 1; # decide whether to show links print h2("Page $page_show of $page_last"); # optional if ($page_show > 1) { # Show a Previous link print a({href => "?page=$page_prev"},"Previous"); } if ($page_show < $page_last) { # Show a Next link print a({href => "?page=$page_next"},"Next"); } # calc start and end user index number starting # page 1 user start is 0, page 2 is 10 # page 1 end is start + 9 unless on last page my $user_start = ($page_show - 1) * 10; my $user_end = ($page_show < $page_last) ? $user_start + 9 : $no_of_ +users - 1 ; # Show the range of entries. for my $i ($user_start .. $user_end) { my $user_to_show = $users[$i]; # user image my $img_src = "$user_to_show/image.jpg"; unless (-r $img_src){ $img_src = 'blank_profile.jpg'; } print img({src =>$img_src, width=>150, height=> 150 }); # user profile my $profile_filename = "$user_to_show/profile"; if (open my $p, $profile_filename){ my $profile = join '<br/>', <$p>; close $p; print $profile; } else { print "Can not open $profile_filename : $!"; } print br; } }
    poj