Solved

Previous/Next Links in Perl Script - MySQL Select

Posted on 2009-04-08
2
484 Views
Last Modified: 2013-12-25
I'm setting up a simple guestbook in Perl and want to include Next/Previous links at the bottom of the SQL data table.

This was covered in two prior E-E posts...
/Programming/Languages/Scripting/Perl/Q_20435798.html
/Programming/Languages/Scripting/Perl/Q_10311853.html

...but I can't follow the pseudocode well enough to apply it.

It seems logical that I'll need a combo of IF (Perl) and LIMIT (SQL), but can anyone take a closer look and advise?

Thanks!
#!/usr/bin/perl
 
$table = "guestbook";
 
<<DBI connections and modules/libraries>>
 
sub guestbook {
$count = 1;
  print "<TABLE BORDER=1>\n";
  print "<TR><TD>No.</TD><TD>Name</TD><TD>Date</TD>";
  print "<TD>Email</TD><TD>Message</TD></TR>\n";
  &run_statement("select * from $table;");
  while (($name, $entry_date, $email, $message) = $sth->fetchrow) {
    print "<TR><TD>$count</TD><TD>$name</TD><TD>$entry_date</TD>";
    print "<TD>$email</TD>";
    print "<TD>$message</TD></TR>\n";
  $count++;
  }
  print "</TABLE>\n";
}

Open in new window

0
Comment
Question by:mcantera25
2 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24126642
You will need a way for the user to select the next page, then an update to your SQL statement to only get some of the results.  Because records are not normally ordered, the only to ensure that the records you get are the right ones will be to include an ORDER BY in your SQL statement.  Something like so:
sub guestbook {
  #This needs to come from the "first" field below
  #It should initialize to 0
  my $first = shift;
$count = 1;
  print "<TABLE BORDER=1>\n";
  print "<TR><TD>No.</TD><TD>Name</TD><TD>Date</TD>";
  print "<TD>Email</TD><TD>Message</TD></TR>\n";
  &run_statement("select * from $table ORDER BY id LIMIT $first,10;");
  while (($name, $entry_date, $email, $message) = $sth->fetchrow) {
    print "<TR><TD>$count</TD><TD>$name</TD><TD>$entry_date</TD>";
    print "<TD>$email</TD>";
    print "<TD>$message</TD></TR>\n";
  $count++;
  }
  print "</TABLE>\n";
  print "<a href=\"link_to_this_page.pl?first=" . ($first+10) . "\">Next page</a>\n";
}

Open in new window

0
 

Author Closing Comment

by:mcantera25
ID: 31568356
Thanks so much, especially for using "real" code. Made it really helpful and easy to implement.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Facebook React and SyntheticEvents 6 76
Interactive Script in Scheduled Task not running 8 47
Need to combine two scripts 2 41
convert Systemjs to Webpack 3 83
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question