Index
ch07 (16)
Issac Asimov Nemesis (2)
GAIMAN~1 (2)
K. J. Yeskov Ostatni Wladca Pierscienia (2)
Paulo Coelho Weronika postanawia umrzec
rozdzial 06 (63)
181 12 (8)
473 08
06 (179)
lex polonica 6
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • alheo.htw.pl

  • [ Pobierz całość w formacie PDF ]
    .= "<HR>$year $make $model<BR>$description<BR>Asking\$$price<BR>Contact: $contact\n";$count++;}}}}}$search = "$count entries matched your search for <B>$data{'year'}$data{'make'} $data{'model'}</B>.\n";open(TEMPLATE, "$tmpl_file") || die "Content-type: text/html\n\nCannotopen template!";@template = <TEMPLATE>;close(TEMPLATE);$template[6] =~ s/XXXX/$search/e;$template[7] =~ s/YYYY/$matched/e;print "Content-type: text/html\n\n";print @template;}Putting Together the Search ScriptNow that you have completed the Search subroutine, you have all the pieces for your search script.You just need to combine the Search subroutine with the No_SSI and User_Data subroutines in a file called search.pl.Listing 7.18 contains all the Perl codefor the search script.You need to change the value for the $path variable to the path to the database and template files on your machine.Also, if you will be using this script on a Windows machine, you should remove the first line of code.Figure 7.6shows the results of a sample search, where the user entered the string Honda for the make to search for.Listing 7.18: The search.pl File#!/usr/local/bin/perl# All users need to change the $path# variable to the path to the database# and template files on their machine.# Windows users need a path in the form# $path = "c:\\robertm\\";$path = "/users/robertm/";$database = $path."car.dat";$tmpl_file = $path."results.tmpl";%data_received = &User_Data;&No_SSI(*data_received);if ($ENV{'REQUEST_METHOD'} eq "POST") {&Search(%data_received);} else {print "Content-type: text/html\n\nYou are not using this script correctly!";}sub Search {local (%data) = @_;local ($count) = Ø;local (@database, $id, $year, $make, $model, $description, $price, $contact,@template, $matches, $search);open(DATABASE, "$database") || die "Content-type: text/html\n\nCannot opendatabase!";@database = <DATABASE>;close(DATABASE);foreach (@database) {($id, $year, $make, $model, $description, $price, $contact) = split(/::/);if (($make =~ /\b$data{'make'}/i) || ($data{'make'} =~ /\b$make/i)) {# make matched, check modelif (($model =~ /\b$data{'model'}/i) || ($data{'model'} =~ /\b$model/i)|| $data{'model'} eq "") {# model matched, check yearif ( (($data{'lowyear'} <= $year) && ($data{'highyear'} >= $year)) ||(($data{'lowyear'} eq "") && ($data{'highyear'} eq "")) ) {# year is in range, check priceif ( (($data{'lowprice'} <= $price) && ($data{'highprice'} >= $price))|| (($data{'lowprice'} eq "") && ($data{'highprice'} eq "")) ) {# price matched, add entry to user's list of matches$matched.= "<HR>$year $make $model<BR>$description<BR>Asking\$$price<BR>Contact: $contact\n";$count++;}}}}}$search = "$count entries matched your search for <B>$data{'year'}$data{'make'} $data{'model'}</B>.\n";open(TEMPLATE, "$tmpl_file") || die "Content-type: text/html\n\nCannotopen template!";@template = <TEMPLATE>;close(TEMPLATE);$template[6] =~ s/XXXX/$search/e;$template[7] =~ s/YYYY/$matched/e;print "Content-type: text/html\n\n";print @template;}sub No_SSI {local (*data) = @_;foreach $key (sort keys(%data)) {$data{$key} =~ s/<!--(.|\n)*-->//g;}}sub User_Data {local (%user_data, $user_string, $name_value_pair,@name_value_pairs, $name, $value);# If the data was sent via POST, then it is available# from standard input.Otherwise, the data is in the# QUERY_STRING environment variable.if ($ENV{'REQUEST_METHOD'} eq "POST") {read(STDIN,$user_string,$ENV{'CONTENT_LENGTH'});} else {$user_string = $ENV{'QUERY_STRING'};}# This line changes the + signs to spaces.$user_string =~ s/\+/ /g;# This line places each name/value pair as a separate# element in the name_value_pairs array.@name_value_pairs = split(/&/, $user_string);# This code loops over each element in the name_value_pairs# array, splits it on the = sign, and places the value# into the user_data associative array with the name as the# key.foreach $name_value_pair (@name_value_pairs) {($name, $value) = split(/=/, $name_value_pair);# These two lines decode the values from any URL# hexadecimal encoding.The first section searches for a# hexadecimal number and the second part converts the# hex number to decimal and returns the character# equivalent.$name =~s/%([a-fA-FØ-9][a-fA-FØ-9])/pack("C",hex($1))/ge;$value =~s/%([a-fA-FØ-9][a-fA-FØ-9])/pack("C",hex($1))/ge;# If the name/value pair has already been given a value,# as in the case of multiple items being selected, then# separate the items with a " : ".if (defined($user_data{$name})) {$user_data{$name}.= " : ".$value;} else {$user_data{$name} = $value;}}return %user_data;}Interacting with complex DBMS programsSo far in this chapter, you have been interacting with a text file database.Although this example is useful to illustrate the interaction of the Web with a database through CGI, it's unlikely that you have text file based databases.If you have anydatabases, they are probably in a highly specialized, complex DBMS program such as Oracle, Sybase, Access, or FileMaker Pro.However, even though these programs are more complicated than a simple text file database, you still interface them to the Web witha CGI script.Recall from Chapter 2 that CGI defines how data passes back and forth between your Web pages and your CGI scripts.When interacting with a DBMS program, you want to take the information obtained from your Web page and get it to your DBMS [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • aceton.keep.pl
  • 
    Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl.