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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying ten results per page in perl cgi

Status
Not open for further replies.

squish20

Programmer
May 25, 2012
1
AU
Hi

I'm pretty new to perl and I am currently trying to do an assignment for school which involves us creating a dating website. This requires me to present 10 profiles per page with links to the next and the previous page. This is the subroutine i wrote to handle this:

Perl:
sub browse_ten {
        my $page = 1; # intialise number of pages
        my @users = glob("user_dir/*);
        #entire array of all the user files to be displayed.
        my $pages_required= ceil(($#users)/10); #calculate how many pages to display
        while ($page le $pages_required) {
                my $start = ($page - 1) * 10; # index to begin displaying 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 shown
                        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 open $profile_filename: $!\n";
                        if(-r "$user_to_show/image.jpg"){
                                print img({src=>"$user_to_show/image.jpg", width =>150, height=>$150});
                        } else {
                                print img({src=>"blank_profile.jpg", width=>150, height=>150});
                        }
                        $profile = join '<br>', <$p>; $profile .= "<br>";
                close $p;
                print $profile;
                }
                $page = $page+1;



It previously kept giving me the error that it :Can't modify constant item in scalar assignment at ./trial3.cgi line 37, near ""\">Previous</a>";"

I tried to fix that by replacing the '=' with '=>' in the href lines.
It still gives me a syntax error on those lines which i dont quite seem to be able to fix.

Also there are 2 missing braces in the function coz perl is being silly and complains that there are extra curly braces when i put the right number of braces in [mad]

I was hoping someone could help me figure out what's wrong with that statement.
 
Hi

squish20 said:
I tried to fix that by replacing the '=' with '=>' in the href lines.
Huh ? What is a "href line" ?

Anyway, your first syntax error is in the 3[sup]rd[/sup] line, the missing closing double quote ( " ) :
Code:
[b]my[/b] [navy]@users[/navy] [teal]=[/teal] [b]glob[/b][teal]([/teal][green][i]"user_dir/*[highlight]"[/highlight][/i][/green][teal]);[/teal]

Such errors are clearly visible if you use an editor able to highlight the syntax. Please use one. See faq219-5835 for a brief list.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top