×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Printing "X" amount of records

Printing "X" amount of records

Printing "X" amount of records

(OP)
I have written a script that reads a database among other things. My problem is this. As the script is now, it'll read the entire database and print out each line (of the entire database) nicely formatted (using the FOREACH). What I want to do is grab 'x' amount of records, click next, get the next 'x' amount of records until all records are viewed.

How would I go about doing this? I'm fairly new to perl programming from scratch and would appreciate any help.

RE: Printing "X" amount of records

I pretty sure my way is not the best way, as it performs a complete database lookup each time, but it works for me.  if someone has a better way, please post it.
 basically, i have the script with a couple extra variables, $start_index and $page_limit, which are passed to the script from the calling page, or set automatically.  it prints "$page_limit" number of entries from the point "$start_index" on.  This is assuming the database has been put in an array form (which i call @data).  this script example also assumes that the the html formdata was parsed into a hash(%formdata).
 then, for the previous and next buttons, it increments or decrements the "$start_index" by "$page_limit", and has a form submitted to itself.  if other variables are needed by the cgi, add a "hidden" input to the bottom form for each one.

#the database calls and basic cgi stuff that you already
#have would be above, but not the foreach loop...

my $start_index = ( $formdata{start_index} || 0 );
my $page_limit = ( $formdata{page_limit} || 20 );

for (my $i = $start_index; $i <= @data &&
     $i <= $page_limit; $i++)
{
    print $data[$i], "\n";
}


print qq~<FORM METHOD="post" ACTION="???.cgi"
         NAME="Next_Prev">\n~;

if ($start_index > 0 && $start_index % $page_limit == 0)
{
    $start_index -= $page_limit;
    print qq~<INPUT TYPE="submit" NAME="Previous"
                 VALUE="Previous">\n~;
}

if ($start_index + $page_limit < @data)
{
    $start_index += $page_limit;
    print qq~<INPUT TYPE="submit" NAME="Next"
                 VALUE="Next">\n~;
}

print qq~<INPUT TYPE="hidden" NAME="page_limit" VALUE=
         page_limit>\n~;
print qq~<INPUT TYPE="hidden" NAME="start_index" VALUE=
         $start_index>\n</FORM>\n</BODY>\n</HTML>\n~;

 Please note, the print statements that overflow onto two lines really should be only one, and i indented them for readability.  i used qq~~ instead of "" so i wouldn't have to escape the quotes inside.  also, you could hardcode the $page_limit to 20 or whatever if it's never going to change.
hope this works for you.  but actually, i hope someone posts something better.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Printing "X" amount of records

(OP)
Hmmm, I must be doing something wrong. I get several next/previous buttons on one output. Below is my subroutine along with my commented out foreach. Do I still need that? Or am I misnaming my arrays.


sub view {

# Read the database.
open(RESOLVED,"<$done_file") || &ErrorMsg("Could not open file '$done_file'");
@resolved = <RESOLVED>;
close(RESOLVED);

&getdate;
&links;

print "Content-type: text/html\n\n";
print qq~<html>
<head>
<title>$title</title>
</head>

<$body>
$logo

<center><table width="580">
  <tr><td align=center><$font>
    $links
    <br><br>
  <b>Here are the System Administrator Support Requests that have<br>been completed to date:</b>
  <font color=FF9900><b>$date</b></font>
  <br><br><br>
~;

@data = ($date_in,$time_in,$email,$domain,$task,$date_out,$time_out,$resolution,$tech);

my $start_index = ( $data{start_index} || 0 );
my $page_limit = ( $data{page_limit} || 2 );

for (my $i = $start_index; $i <= @resolved &&
     $i <= $page_limit; $i++)
{
    print $resolved[$i], "\n";

#foreach $line (reverse@resolved) {
#    chop($line);
#    ($date_in,$time_in,$email,$domain,$task,$date_out,$time_out,$resolution,$tech)=split(/\|/,$line);
#
#    $task_print = $task;
#    $task_print =~ s/``/<br>/g;
#
#    $resolution_print = $resolution;
#    $resolution_print =~ s/``/<br>/g;
#
#    print qq~
 # <table width="100%"><tr>
#    <td>
#    <table cellspacing=0 cellpadding=0 border=1 width="100%" bordercolor=003366 bgcolor=white>
#      <tr><td>
#        <table width="100%">
#          <tr><td colspan=2><$font><b>Received at:</b> <font color=FF9900>$date_in - $time_in</font></font></td></tr>
#          <tr><td colspan=2><$font><b>$task2:</b> $task_print</font></td></tr>
#        </table>
#        <table width="100%" bgcolor="#EEEEEE">
#          <tr><td><$font><b>Completed at:</b> <font color=FF9900>$date_out - $time_out</font></font></td><td><$font><b>$tech2: </b> $tech</font></td></tr>
#          <tr><td colspan=2><$font><b>$resolution2:</b> $resolution_print</font></td></tr>
#        </table>
#      </td></tr>
#    </table>
#  </td></tr></table>
#  <br>
#    ~;
#}


print qq~<FORM METHOD="post" ACTION="sysresolve2.cgi" NAME="Next_Prev">\n~;

if ($start_index > 0 && $start_index % $page_limit == 0)
{
    $start_index -= $page_limit;
    print qq~<INPUT TYPE="submit" NAME="Previous" VALUE="Previous">\n~;
}

if ($start_index + $page_limit < @resolved)
{
    $start_index += $page_limit;
    print qq~<INPUT TYPE="submit" NAME="Next" VALUE="Next">\n~;
}

print qq~<INPUT TYPE="hidden" NAME="page_limit" VALUE=page_limit>\n~;
print qq~<INPUT TYPE="hidden" NAME="start_index" VALUE=$start_index>~;

}

print qq~
  </font></td></tr>
</table></center>

</body>
</html>
~;
}

RE: Printing "X" amount of records

errr, okay, what the code needs is this:

 the previous and next buttons shouldn't be inside the for loop, they should be printed afterwards.
 also, you seem to want your loop to be applied to the reverse of your data, so do that first.
 as for the body of the for loop, make it exactly what your foreach loop is (a foreach loop is really just a special case of a for loop.  you have to generalize it to only iterate over the specified indexes), except replace every instance of "$line" with "$_", so it'll look something like:
@resolved = reverse @resolved;

for ($i = $start_index; $i <= @resolved &&
     $i <= $page_limit; $i++)
{
    chop($_);
    ($date_in,$time_in,$email,$domain,$task,$date_out,$time_out,$resolution,$tech)=split(/\|/,$_);
#et cetra, et cetra, until the end of all your formatting
#then, end the loop and print out the previous/next button
}

if ($start_index > 0 && blah blah)
{
    #i think you should understand now


 in fact, as i wrote it here, put the rest of your original foreach loop into the for loop, make sure the for loop ends first, then print out the next/previous buttons.  remove the line you had above the loop, assigning @data as you did.  this isn't needed.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close