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!

Paging results from text file 1

Status
Not open for further replies.

brad1978

Programmer
Feb 13, 2009
30
CA
Hi all,
I have a question for you. I am writing user submitted information to a tab delimited text file and reading it back into a web page via ajax. I am reading the text file from the bottom up (newest posts first) and displaying 5 results per page. My problem is in displaying my Previous and Next buttons. If I have less than 5 posts, my page displays a bunch (10) empty display tables. Can someone please guide me in the right direction here? Sorry, the code is pretty long, the issue (I believe) starts around line 60, right after $a = $limit

Code:
#!/usr/bin/perl -wT
#print "Content-type: text/html\n\n";
print header(-expires=>'-1d');
use CGI qw/:standard/;

print "<div style='text-align:right;'><a href='#kudosform' class='black'>Submit a kudos for a colleague</a></div>";

my @tname;
my @temail;
my @say;
my @other;
my @kudos;
my @fname;
my @femail;
my @ts;


	open (FILE, 'posts.txt');
	while (<FILE>) 
    {
      chomp;
      ($toName, $toEmail, $wantedToSay, $other, $kudos, $fromName, $fromEmail, $timestamp ) = split("\t");      
      
       push(@tname, $toName);
       push(@temail, $toEmail);
       push(@say, $wantedToSay);
       push(@other, $other);
       push(@kudos, $kudos);       
       push(@fname, $fromName);
       push(@femail, $fromEmail);
       push(@ts, $timestamp);
 		}
	close (FILE);

my $results = $#tname;
my $limit = undef;
my $nxt = undef;
my $pre = undef;
if(!$ENV{"QUERY_STRING"})
  {
    #print "NO QUERYSTRING ";
    # set limit to the number of results if there's no value from query string.
    $limit = $results;
    #print $limit."<br><br>";
  }
else
  {
    #print "QUERY STRING ";
    # try to get limit value from query string.  Only there if were not on page 1
    $limit = $ENV{"QUERY_STRING"}; 

    #remove the limit= from query string to just obtain value
    $limit = substr($limit, 6);
    #print $limit."<br><br>";
  }


$a = $limit;

if($results > 0)
  {
    print "No kudos found.";
  }
else
  {
    print "<strong>".$results." kudos found</strong>  Browse: ";
    #now print the paging results
    if($limit >= $results)
      {
        $nxt = $limit - 5;
        print "<a href='javascript:void();' title='".$nxt."' class='black' onclick='getNext(".$nxt.");'>next</a>";
      }
    elsif($limit >= 0)
      {
        $nxt = $limit - 5;
        $pre = $limit + 5;
        print "<a href='javascript:void();' title='".$pre."' class='black' onclick='getNext(".$pre.");'>previous</a>";
        if($nxt < -1)
          {
            # do nothing
          }
        else
          {
            print "&nbsp;|&nbsp;<a href='javascript:void();' title='".$nxt."' class='black' onclick='getNext(".$nxt.");'>next</a>";    
          }
      } 
    else
      {
        $pre = $limit + 5;
        print "<a href='javascript:void();' title='".$pre."' class='black' onclick='getNext(".$pre.");'>previous</a>";
      }

    print "<br /><br />";
    while($a > ($limit - 5))
      {
        if($a == 0)
          {
            exit;
          }
         
        print "<style>";
          print "#display { width:100%; text-align:left; } ";
          print ".kudosContainer { width:100%; height:auto; margin:0; padding:0; border: 2px solid #94214b; } ";
          print ".toname { width:10%; height:auto; margin:0; padding:0; float:left; display:inline; border:1px solid white; } ";
          print ".name { width:30%; height:auto; margin:0; padding:0; float:left; display:inline; border:1px solid white; } ";
          print ".fromname { width:10%; height:auto; margin:0; padding:0; float:left; display:inline; border:1px solid white; } ";
          print ".fname { width:30%; height:auto; margin:0; padding:0; float:left; display:inline; border:1px solid white; } ";
          print ".logo { width:10%; height:auto; margin:0; padding:0; float:left; display:inline; border:1px solid white; } ";
          print ".kudos { clear:both; width:100%; height:auto; background-color:#ede3ce; margin:0; padding:0; }";
        print "</style>";
        print "<style media='print'>";
          print ".kudosContainer { width:100%; height:800px; margin:0 0 60px 0; padding:0; border: 2px solid #94214b; }";
          print ".kudos { clear:both; width:100%; height:750px; background-color:#ede3ce; margin:0; padding:0; }";
          print "#display { display:none; } ";
          print "a { display: none; } ";
        print "</style>";
        
        print "<div class='kudosContainer'>";
          print "<div class='toname'><h3>TO:</h3></div>";
          print "<div class='name'><span style='text-decoration:underline';>".@tname[$a]."</span></div>";
          print "<div class='fromname'><h3>FROM:</h3></div>";
          print "<div class='fname'><span style='text-decoration:underline';>".@fname[$a]."</span></div>";
          print "<div class='logo'><img src='/etd/graphics/kudosSm.gif' width='103' height='41' /></div>";
          print "<div class='kudos'><div style='margin:0 5px;'><h3>I wanted to say: </h3><strong>";
          if(@say[$a] eq "Thanks")
                {
                  print "Thanks, because...";
                }
              elsif (@say[$a] eq "congrats")
                {
                  print "Congratulations because...";
                }
              elsif (@say[$a] eq "fabjob")
                {
                  print "Fabulous job because...";
                }              
              elsif (@say[$a] eq "hero")
                {
                  print "You're my hero, because...";
                }
              elsif (@say[$i] eq "rockstar")
                {
                  print "You're a rock star, because...";
                }
              elsif (@say[$a] eq "madeday")
                {
                  print "You made my day, because...";
                }
              elsif (@say[$a] eq "trooper")
                {
                  print "You're a real trooper, because...";
                }
              elsif (@say[$a] eq "fab")
                {
                  print "You're fab, because...";
                }
              elsif (@say[$a] eq "right")
                {
                  print "You've got the right stuff, because...";
                }
              elsif (@say[$a] eq "other")
                {
                  print @other[$a].", because...";
                }
             else
                {
                 print "Thanks, because...";
                }
          print "</strong><br />".@kudos[$a]."<br /><div align='right'><a href='/cgi-etd/kudos/printkudos.cgi?id=".$a."' id='print' target='_blank' class='black'>Print Friendly</a></div></div></div>";
        print "</div><br />";   
        $a--;
      }
  }

Any help is much appreciated!
Thanks
 
I saw CGI::pager recently which seems to handle this sort of thing easily:
I've always just written the code myself. Something like...

Code:
my $page = 2; # get this from the user

# @entries is the entire array of what could be displayed

my $start = ($page - 1) * 10; # index to begin displaying them
my $end   = $start + 10; # show 10 results per page
$end = scalar(@entries) if $end > scalar(@entries); # cap the end

# whether to show links
if ($start > 0) {
   # Show a Previous link
   print "<a href=\"page.cgi?page=" + ($page - 1) + "\">Previous</a>";
}
if (scalar(@entries) > $end) {
   # There's more entries after what's being shown
   print "<a href=\"page.cgi?page=" + ($page + 1) + "\">Next</a>";
}

# Show the range of entries.
for (my $i = $start; $i < $end; $i++) {
   my $entry = $entries[$i];
   print "$entry<br>\n"; # or w/e
}

So when $page = 1, $start = (1-1)*10 which is 0 and $end = 0+10 = 10, so results 0-9 are displayed... when $page = 2, $start = (2-1)*10 = 10 and $end = 10+10=20, so results 10-19 are displayed... etc.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirsle,
Thanks sooooo much for the help! It took me a while to get your pseudo code to work, but it was a huge help. Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top