| |
| Dynamically create a FlipBook using Perl |
|
This chapter describes how you can create FlipViewer® books dynamically by linking them to
your database using the Perl scripting language.
|
| |
|
Before reading this tutorial, you will need to have an understanding of the Perl
language and some basic database skills.
|
| |
|
We are assuming you already have an existing database containing data which you want to display
dynamically in FlipViewer®. Thus, we chose this example which illustrates the retrieval of data
from the database and present it in a FlipBook dynamically.
|
| |
|
In this example, we are using Perl 5 as our scripting language and MySQL 3.22 as our database server.
We have a database containing different types of fishes and will like to display these fishes in a nicely formatted
FlipBook. The book will be generated dynamically on the fly each time the OPF is accessed.
|
| |
|
We have developed three Perl scripts for this example. The first is biolife_toc.pl . This script is used to
generate the Table of Contents of all the fishes in the database. The second script is biolife_page.pl .
This script is used to generate the individual HTML pages which contain details of each fish. The last script,
biolife_opf.pl is used to generate the OPF based on the number of fishes in the database.
|
| |
|
We do not know beforehand the number of fishes we have in the database. Thus, we need to use this script,
biolife_toc.pl, to dynamically generate the Table of Contents by retrieving the names of all the fishes from the
database. The script also allows you to sort the Table of Contents either by category or name of the fishes.
|
| |
#!/usr/bin/perl -w
#
# Filename: biolife_toc.pl
#
# Copyright (c) 2000 E-Book Systems, Inc.
#
use DBI;
use CGI;
print "Content-type: text/html\n";
print "pragma: no-cache\n\n";
$host = "http://www.fliplibrary.com";
my $query = new CGI;
my $orderby=$query->param('order');
print "<HTML><BODY BGCOLOR=#FFFFFF SCROLL = NO>";
print "
<TABLE WIDTH=100% BORDER=0 CELLSPACING=0>
<TR BGCOLOR=#ff9933>
<TD colSpan=2 height=20>
<B><CENTER><FONT face='Arial, Helvetica, sans-serif'
size=2><B><I><FONT color=#ffffff size=4>Table of Contents</FONT> </FONT> </CENTER></B>
</TD>
</TR>
<TR><TD colSpan=2 height=10>
";
if ($orderby eq 'C')
{
$sqlst = 'select Species_No, Category, Common_Name from biolife order by Category';
}
else
{
if ($orderby eq 'N')
{
$sqlst = 'select Species_No, Category, Common_Name from biolife order by Common_Name';
}
else
{
$sqlst = 'select Species_No, Category, Common_Name from biolife';
}
}
my $advDB = DBI->connect("dbi:mysql:fliplib", "fliplib", "PASSWORD")
or die "Can't connect to mysql:fliplib: $DBI::errstr\n";
my $advDbst = $advDB->prepare($sqlst)
or die "Can't prepare SQL st: $DBI::errstr\n";
$advDbst->execute
or die "Can't execute SQL st: $DBI::errstr\n";
print '<font face="Verdana, Arial, Helvetica, sans-serif" size="1">';
$i=0;
$currentCat = '';
while(@Records = $advDbst->fetchrow_array())
{
# $advDB->Close();
#exit 0;
$i++;
if (($orderby ne 'C') || $Records[1] ne $currentCat)
{
print "</TD></TR>";
print "<TR><TD VALIGN=TOP width='32%'>";
print '<font color=#0066cc face="Verdana, Arial, Helvetica, sans-serif" size="1">';
print "\n<B>$Records[1]</B> ";
print "</FONT>";
print "</TD>";
print "<TD VALIGN=TOP>";
$currentCat = $Records[1];
}
print "<font face='Verdana, Arial, Helvetica, sans-serif' size='1'>\n";
print "\n<A href = '";
print "$host/cgi/DynBooks/biolife_page.pl?pid=".$Records[0]."'>";
print "$Records[2]</A>, \n";
print "</FONT>\n";
}
print "</TR></FONT>";
print "</TABLE>\n";
print "</BODY></HTML>";
$advDB->disconnect;
exit 0;
|
|
| |
|
After generating the Table of Contents, we are going to generate the individual HTML pages describing
the details of each fish. The following script, biolife_page.pl, is used to generate a HTML page of the selected fish.
|
| |
#!/usr/bin/perl -w
#
# Filename: biolife_page.pl
#
# Copyright (c) 2000 E-Book Systems, Inc.
#
use DBI;
use CGI;
print "Content-type: text/html\n";
print "pragma: no-cache\n\n";
$query = new CGI;
$pid=$query->param('pid');
#$pid="90020.0";
my $advDB = DBI->connect("dbi:mysql:fliplib", "fliplib", "PASSWORD")
or die "Can't connect to mysql:fliplib: $DBI::errstr\n";
$sqlst = 'select * from biolife ';
$sqlst .= " where '$pid' = Species_No";
my $advDbst = $advDB->prepare($sqlst)
or die "Can't prepare SQL st: $DBI::errstr\n";
$advDbst->execute
or die "Can't execute SQL st: $DBI::errstr\n";
$i=0;
@Records = $advDbst->fetchrow_array();
$file = './biolife_page.tpl';
open (IDFILE, "$file") || print "Open $file failed!";
while (<IDFILE>)
{
$rec = $_;
$rec =~ s/##CATEGORY##/$Records[1]/g;
$rec =~ s/##COMMONNAME##/$Records[2]/g;
$rec =~ s/##NOTES##/$Records[6]/g;
$rec =~ s/##GRAPHIC##/$Records[7]/g;
print $rec;
}
close (IDFILE);
$advDB->disconnect;
exit 0;
|
|
| |
|
The template below, biolife_page.tpl is used by biolife_page.pl to display the HTML page.
|
| |
<HTML>
<HEAD>
<TITLE>Biolife Data</TITLE>
</HEAD>
<BODY scroll = "no">
<TABLE BORDER=0 >
<TR BGCOLOR=#ff9933>
<TD colSpan=2 height=20 VALIGN=CENTER><FONT color=#ffffff face="Verdana, Arial, Helvetica, sans-serif" size=1><B>Biolife Catalogue Data</B></FONT>
</TD>
</TR>
<TR>
<TD width="44%">
<font color = "#0066cc" face="Verdana, Arial, Helvetica, sans-serif" size="2">
<B>Category:</B>
</font>
</TD>
<TD>
<font color = "#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2">
##CATEGORY##
</font>
</TD>
</TR>
<TR>
<TD width="44%">
<font color = "#0066cc" face="Verdana, Arial, Helvetica, sans-serif" size="2">
<B>Common Name:</B>
</font>
</TD>
<TD>
<font color = "#000000" face="Verdana, Arial, Helvetica, sans-serif" size="2">
##COMMONNAME##
</font>
</TD>
</TR>
<TR>
<TD COLSPAN= 2 VALIGN=TOP>
<font color = "#0066cc" face="Verdana, Arial, Helvetica, sans-serif" size="2">
<B>Notes:</B>
</font>
</TD>
</TR>
<TR>
<TD COLSPAN= 2>
<font face="Verdana, Arial, Helvetica, sans-serif" size="1">
##NOTES##
</FONT>
</TD>
</TR>
<TR>
<TD COLSPAN= 2 VALIGN=TOP>
<font color = "#0066cc" face="Verdana, Arial, Helvetica, sans-serif" size="2">
<B>Picture:</B>
</font>
</TD>
</TR>
<TR>
<TD COLSPAN= 2><CENTER><IMG SRC="##GRAPHIC##" alt="##COMMONNAME##" border="0"></CENTER></TD>
</TR>
</TABLE>
</BODY>
</HTML>
|
|
| |
|
The final script, biolife_opf.pl, is used to generate the OPF. This script uses the above two scripts
to generate the Table of Contents and the individual detail HTML pages during the process of generating
the OPF file. It passes a sort by parameter to biolife_toc.pl script to sort the Table of Contents either
by category or name of the fishes. The script also passes a pid parameter to biolife_page.pl script to
display the details of the selected fish in a dynamically generated HTML page.
|
| |
#!/usr/bin/perl -w
#
# Filename: biolife_opf.pl
#
# Copyright (c) 2000 E-Book Systems, Inc.
#
print "Content-type: Application/x-opf\n";
print "pragma: no-cache\n";
use DBI;
use CGI;
$query = new CGI;
$orderby=$query->param('order');
print '\n\n<HEAD> <META HTTP-EQUIV="Expires" CONTENT="Tue, 04 Dec 1993 21:29:02 GMT"></HEAD>'; my $advDB = DBI->connect("dbi:mysql:fliplib", "fliplib", "PASSWORD")
or die "Can't connect to mysql:fliplib: $DBI::errstr\n";
$sqlst = 'select "Species_No" from biolife';
my $advDbst = $advDB->prepare($sqlst)
or die "Can't prepare SQL st: $DBI::errstr\n";
$advDbst->execute
or die "Can't execute SQL st: $DBI::errstr\n";
$noRecords = 0;
while (@Records = $advDbst->fetchrow_array())
{
$item[$noRecords] = $Records[0];
$noRecords++;
}
if ($orderby eq 'C')
{
$sqlst = 'select Species_No from biolife order by Category';
}
else
{
if ($orderby eq 'N')
{
$sqlst = 'select Species_No from biolife order by Common_Name';
}
else
{
$sqlst = 'select Species_No from biolife';
}
}
my $advDbst = $advDB->prepare($sqlst)
or die "Can't prepare SQL st: $DBI::errstr\n";
$advDbst->execute
or die "Can't execute SQL st: $DBI::errstr\n";
$items = "<item id = 'itemTOC' href = '";
$items .= $host."/cgi/DynBooks/biolife_toc.pl?order=";
$items .= $orderby."' media-type='text/html'/>";
$spine = "\n<itemref idref = 'itemTOC'/>";
$i=0;
while (@Records = $advDbst->fetchrow_array())
{
$i++;
$items .= "\n<item id = ".'"item'.$i.'" href = "';
$items .= $host."/cgi/DynBooks/biolife_page.pl?pid=".$Records[0].'"'." media-type='text/html'/>";
$spine .= "\n<itemref idref = ".'"item'.$i.'"/>';
}
$file = './DynamicBook.tpl';
open (IDFILE, "$file") || print "Open $file failed!";
while (<IDFILE>)
{
$rec = $_;
$rec =~ s/##ITEMS##/$items/g;
$rec =~ s/##SPINE##/$spine/g;
print $rec;
}
close (IDFILE);
$advDB->disconnect;
exit 0;
|
|
| |
| The following template, DynamicBook.tpl, is used by biolife_opf.pl to generate the OPF. |
| |
|
<?xml version = "1.0" encoding = "UTF-8"?>
<package unique-identifier = "unspecified">
<metadata>
<x-metadata>
<meta name = "centerfold" content = "no"/>
</x-metadata>
</metadata>
<manifest>
<item id = "item00" href = "http://www.fliplibrary.com/System/cvFront.flp" media-type = "application/x-flp"/>
##ITEMS##
<item id = "item01" href = "http://www.fliplibrary.com/System/cvBack.flp" media-type = "application/x-flp"/>
</manifest>
<spine>
<itemref idref = "item00" index = "No"/>
##SPINE##
<itemref idref = "item01" index = "No"/>
</spine>
</package>
|
|
| |
|
After the above scripts are uploaded to your web server, enter the URL to your biolife_opf.pl script in
FlipViewer® to display our dynamic Fish FlipBook.
|
| |
|
The complete source code and database structure can be obtained
here
in zip format.
|
| |
|
If you have any further questions, please email us at:
webmaster@FlipViewer®.com
|
| |
| >> Back to Content
|
|
|
|
 |