squish20 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm trying to design a dating website for a school assignment given a synthetic data set. I was trying to display 10 profiles per page with links to the next and previous page. I've tried a whole heap of things but it always seem to get some sort of error on the href lines. I was hoping someone could point out where i went wrong. Also, Perl seems to complain that i have unmatched curly braces when i had the right number of braces. Removing the ending brace for the while loop and the subroutine seemed to have fixed that but now i'm actually missing two braces! Here's my subroutine:

sub browse_ten { my $page = 1; # intialise number of pages my @users = glob("users_dir/*); #entire array of all the user files to be displayed. my $pages_required= ceil(($#users)/10); #calculate how many pa +ges to display while ($page le $pages_required) { my $start = ($page - 1) * 10; # index to begin display +ing them my $end = $start + 10; # show 10 results per page $end = scalar(@users) if $end > scalar(@users); # cap +the end # whether to show links if ($start > 0) { # Show a Previous link print "<a href=\"engcupid.cgi?page=" + ($page +- 1) + "\">Previous</a>"; } if (scalar(@users) > $end) { # There's more entries after what's being show +n print "<a href=>\"engcupid.cgi?page=" + ($page + + 1) + "\">Next</a>"; } # Show the range of entries. for (my $i = $start; $i < $end; $i++) { my $user_to_show = $users[$i]; $profile_filename = "$user_to_show/profile"; open $p, "$profile_filename" or die "cannot op +en $profile_filename: $!\n"; if(-r "$user_to_show/image.jpg"){ print img({src=>"$user_to_show/image.j +pg", width =>150, height=>$150}); } else { print img({src=>"blank_profile.jpg", w +idth=>150, height=>150}); } $profile = join '<br>', <$p>; $profile .= "<br>"; close $p; print $profile; } $page = $page+1;

Replies are listed 'Best First'.
Re: displaying 10 results per page
by kcott (Archbishop) on May 26, 2012 at 05:12 UTC

    The second line of sub browse_ten has a mismatched double-quote. This is probably hiding your braces inside a string (which traverses several lines ending with  ... print ").

    Change

    my @users = glob("users_dir/*);

    to

    my @users = glob("users_dir/*");

    and replace the braces you removed.

    -- Ken

      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?

        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

        Is there something wrong with my logic?

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

        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
Re: displaying 10 results per page
by CountZero (Bishop) on May 26, 2012 at 19:32 UTC
    CPAN is your friend: Data::Page

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics