Solved

Previous/Next Links in Perl Script - MySQL Select

Posted on 2009-04-08
2
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

688 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