CGI and Perl

Build HTML from Data on a Single Item in the Database

The program in Listing 8.4 uses DBI to query the database about a specific item and to extract the various information provided in the raw data we input earlier into the database. The data is formatted into HTML output in a similar fashion as in Listing 8.3. Figure 8.2 shows the HTML output generated by this program. Notice CGI.pm is used here to parse the URI encoded data containing the call_num of the item to look up. There are four main steps in the program in Listing 8.4.

  1. line 20 The driver (DBD) for mSQL is installed.

  2. line 21 DBI is instructed to connect to the database named AV_LIBRARY.

  3. line 25 The prepare statement is invoked.

  4. line 30 The execute statement is invoked.

Listing 8.4. Program that writes HTML from data about a single item in the database.
1:   #!/usr/local/bin/perl
 2:   #
 3:   #  Display an item from the catalog table in the AV_LIBRARY database
 4:   #
 5:   #    by Tom White
 6:   #
 7:   #    $Id: display.cgi,v 1.4 1996/08/28 13:42:06 avlibrar Exp avlibrar $
 8:   open (STDERR, ">&STDOUT");
 9:   select STDERR; $| = 1;
 10:  select STDOUT; $| = 1;
 11:  use CGI;
 12:  $ENV{`PATH'} = `/usr/local/bin:/usr/ucb:/usr/bin:/usr/ccs/bin:/usr/sbin';
 13:  $ENV{`MANPATH'} =`/usr/local/man:/usr/man';
 14:  $ENV{`PAGER'} = `/usr/bin/cat';
 15:  $ENV{`SHELL'} = `/bin/sh';
 16:  $ENV{`IFS'} = ``;
 17:  $query = new CGI;
 18:  &pheader;
 19:  use DBI;
 20:  $drh = DBI->install_driver( `mSQL' ) || die "Could not install: $DBI::errstr";
 21:  $dbh = $drh->connect( ``, `AV_LIBRARY' )
 22:            || die "Could not connect: $DBI::errstr";
 23:  $call_num = uc $query->param(`cn');
 24:  $sstring = "call_num = \'$call_num\'";
 25:  $sth = $dbh->prepare( "SELECT title, subtitle, type, description,
 26:                                publisher, narrative, kinder, primer,
 27:                                element, junior, senior, college, adult
 28:                         FROM catalog WHERE $sstring" )
 29:           || die "Could not start select: $DBI::errstr";
 30:  $sth->execute || die "Could not select: $DBI::errstr";
 31:  print "<dt><b>Call Number</b> <dd>$call_num\n";
 32:  if ( ($title, $subtitle, $type, $description, $publisher,
 33:        $narrative, $kinder, $primer, $element, $junior,
 34:        $senior, $college, $adult) = $sth->fetchrow ) {
 35:      print "<dt><b>Title</b> <dd>$title\n";
 36:      if ( $subtitle ) {
 37:        print "<dt><b>Subtitle</b> <dd>$subtitle\n";
 38:      }
 39:      print "<dt><b>Medium</b> <dd>$type";
 40:      if ( $description ) {
 41:        print " ($description)";
 42:      }
 43:      print "\n";
 44:      print "<dt><b>Publisher</b> <dd>$publisher\n";
 45:      print "<dt><b>Narrative</b> <dd>$narrative\n";
 46:      print "<dt><b>Audience</b>";
 47:      print " <dd>Kindergarten (ages 3-5)" if ( $kinder );
 48:      print " <dd>Primary (grades 1-3)" if ( $primer );
 49:      print " <dd>Elementary (grades 4-6)" if ( $element );
 50:      print " <dd>Junior High (grades 7-9)" if ( $junior );
 51:      print " <dd>Senior High (grades 10-12)" if ( $senior );
 52:      print " <dd>College" if ( $college );
 53:      print " <dd>Adult" if ( $adult );
 54:  } else {
 55:      print "<br><b>is NOT on file.</b>";
 56:  }
 57:  print "\n";
 58:  $sth->finish;
 59:  &pfooter;
 60:  exit;
 61:  sub pheader {
 62:    print $query->header;
 63:    print <<EOD;
 64:  <HTML>
 65:  <HEAD>
 66:    <TITLE>MCES's Audiovisual Reference Room - Item Listing</TITLE>
 67:    <LINK REV="MADE" HREF="mailto:avlibrar\@ces.msstate.edu">
 68:    <META NAME="MarkUp" CONTENT="Tom White">
 69:  </HEAD>
 70:  <BODY>
 71:  <h1><img src="/pics/ces-b1mt.gif" width=499 height=53
 72:           alt="Cooperative Extension Service -
 73:                Mississippi State University">
 74:  </h1>
 75:  <h2>
 76:    Audiovisual Reference Room
 77:  </h2>
 78:  <h3>
 79:    Item Listing
 80:  </h3>
 81:  <dl>
 82:  EOD
 83:  }
 84:  sub pfooter {
 85:    print <<EOD;
 86:  </dl>
 87:  <HR>
 88:    [<a href="./">Audiovisual Reference Room</a>]
 89:    <br>
 90:    [<a href="/ces.html">Cooperative Extension</a>]
 91:    [<a href="http://www.msstate.edu/">Mississippi State</a>]
 92:    [<a href="http://www.msstate.edu/web/search.htm">Search MSU's Web</a>]
 93:    <br>
 94:    <FONT SIZE="-1">
 95:    For information about this page, contact
 96:       <a href="mailto:avlibrar\@ces.msstate.edu">avlibrar\@ces.msstate.edu</A>.
 97:    <br>
 98:    <b>Last modified:</b> 08-10-96
 99:    <br>
 100:   <A HREF="http://www.msstate.edu/web/disclaim.htm">Mississippi State
        University is an equal opportunity institution.</A>
 101:   </FONT>
 102:  </BODY>
 103:  </HTML>
 104:  EOD
 105: }

Output from listing 8.4

Figure 8.2. Output from Listing 8.4.

by BrainBellupdated
Advertisement: