Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: help? multiple page results

    click here to see what its doing. http://perl-lib.com/cgi-bin/test.pl

    i don't know whats wrong with this.


    #!/usr/bin/perl -wT

    print ("Content-type: text/html\n\n");

    $path = "list.txt";


    parse();
    show_pages($path);

    # Gets the total number of pages.
    # 10 represents how many per page.
    # Example: my $list = getpages($array, 10);
    sub getpages {
    my($num, $perpage) = @_;
    $num++;
    if(($num/$perpage) =~ /([0-9]+)\./){
    return ($1+1);
    } else {
    return ($num/$perpage);
    }
    }


    # Get the page's result range.
    # Example: ($rs, $re) = getrange($array, $page, 10);
    sub getrange {
    my($num, $page, $perpage) = @_;
    my($rs, $re);
    $page = 1 if(!$page || $page == 0);
    if($page == 1){
    $perpage--;
    $rs = '0';
    $re = $perpage;
    } else {
    $re = $page * $perpage;
    $rs = $re - $perpage;
    if($re > $num) {
    $re = $num;
    }
    $re -= 1 if($re != $num);
    }
    if($rs && $re) {
    return ($rs, $re);
    } else {
    return (0, $perpage);
    }
    }


    # Trims any crap off of a string that is not needed.
    # Example: $string = trim_crap($string);
    sub trim_crap {
    my($string) = shift(@_);
    my($junk) = " HTTP/1.0";
    $string =~ s/$junk//g;
    $junk = " HTTP/1.1";
    $string =~ s/$junk//g;
    $junk = " INCLUDED";
    $string =~ s/$junk//g;
    return $string;
    }


    sub trim_advanced {
    my($string) = shift(@_);
    $string =~ s/\n//g;
    $string =~ s/\r//g;
    $string =~ s/\t/ /g;
    $string =~ s/\f//g;
    $string =~ s/\\v//g;
    $string =~ s/\b//g;
    return $string;
    }


    # Get all the incoming variables/values and store them in $in{variable}.
    # Parses any type of query string. Get, Post, Regular, and Plain text.
    # Examples: &parse; &parse($seperator);
    sub parse {
    my ($seperator) = @_ if @_;
    my ($passed, $junk, $url, $query, $pair, $name, $value);
    if (not(defined($seperator))) {
    $seperator .= "\0";
    }
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    if (length($ENV{'SERVER_PROTOCOL'}) > 0) {
    $passed = $ENV{'REQUEST_URI'} . " " . $ENV{'SERVER_PROTOCOL'};
    $passed = trim_crap($passed);
    ($url, $query) = split(/\?/, $passed);
    @pairs = split(/&/, $query);
    }
    else {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
    }
    }
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
    }
    else {
    if (length($ENV{'SERVER_PROTOCOL'}) > 0) {
    $passed = $ENV{'REQUEST_URI'} . " " . $ENV{'SERVER_PROTOCOL'};
    $passed = trim_crap($passed);
    ($url, $query) = split(/\?/, $passed);
    @pairs = split(/&/, $query);
    }
    else {
    @pairs = split(/&/, $ENV{'DOCUMENT_URI'});
    }
    }
    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name = trim_crap($name);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;
    $value = trim_crap($value);
    chomp($value); chomp($name);
    if (defined($in{$name})) {
    $in{$name} .= $seperator;
    }
    $in{$name} .= $value;
    }
    }


    sub show_pages {
    my($path) = shift(@_);
    my($results) = 10; #how many a page?
    my($pagenum, $pages, $offset, $end);
    umask (000);
    use Fcntl;
    sysopen(FILE, "$path", O_RDWR|O_NONBLOCK|O_CREAT, 0777);
    my(@contents) = <FILE>;
    close(FILE);
    $pagenum = $in{'page'};
    $pages = getpages($contents, $results);
    ($offset, $end) = getrange($contents, $pagenum, $results);
    print "<font style=\"font:7.5pt verdana\">\n";
    foreach my $items ($offset .. $end) {
    my $list = trim_advanced($contents[$items]);
    print "$list\n";
    }
    print "\n";
    if($pagenum > 1) {
    print "<a href=?page=" . ($pagenum - 1) . ">Back</a>";
    print " ";
    }
    if($pagenum < $pages) {
    print "<a href=?page=" . ($pagenum + 1) . ">Next</a>";
    }
    print "\n</font>\n";
    }

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167

    Re: help? multiple page results

    You should use the CGI-Module...

    Code:
    #!/usr/bin/perl -wT 
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    
    my $cgi = CGI->new();
    my %params = $cgi->Vars(); # your sub parse is replaced by this line
    
    print $cgi->header(-type => 'text/html');
     
    my $path = "list.txt"; 
    show_pages($path); 
    
    # Gets the total number of pages. 
    # 10 represents how many per page. 
    # Example: my $list = getpages($array, 10); 
    sub getpages { 
     my($num, $perpage) = @_; 
      unless($num % $perpage){ 
        return ($1+1); 
      } else { 
       return ($num/$perpage); 
      } 
    }
     
    sub trim_advanced { 
      my($string) = shift(@_); 
      $string =~ s/\n//g; 
      $string =~ s/\r//g; 
      $string =~ s/\t/ /g; 
      $string =~ s/\f//g; 
      $string =~ s/\\v//g; 
      $string =~ s/\b//g; 
      return $string; 
    } 
    
    sub show_pages { 
      my($path) = shift(@_); 
      my($results) = 10; #how many a page? 
      my($pagenum, $pages, $offset, $end); 
      umask (000); 
      use Fcntl; 
      sysopen(FILE, "$path", O_RDWR|O_NONBLOCK|O_CREAT, 0777); 
      my(@contents) = <FILE>; 
      close(FILE); 
      $pagenum = $params{'page'} || 1; 
      my $entries = scalar(@contents);
      $pages = getpages($entries, $results); 
      my $start = (($pagenum - 1) * $results);
      my $stop = ($pagenum * $results) - 1;
      print "<font style=\"font:7.5pt verdana\">\n"; 
      foreach my $items ($start .. $stop) { 
       my $list = trim_advanced($contents[$items]); 
       print "$list\n"; 
      } 
       print "\n"; 
       if($pagenum > 1) { 
        print "<a href=thisscript.cgi?page=" . ($pagenum - 1) . ">Back</a>"; 
        print " "; 
      } 
       if($pagenum < $pages) { 
        print "<a href=thisscript.cgi?page=" . ($pagenum + 1) . ">Next</a>"; 
      } 
        print "\n</font>\n"; 
    }
    This code is untested. If you experience any problems, please post the errormessages.
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Mar 2004
    Posts
    2
    Thank you so much. I was going to try to use Data::Page if this didn't work out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •