Chapter 12: Searching in Web applications The first examples use a search form embedded in a Web page to query the deptstore database, which contains the.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Chapter 6 UNDERSTANDING AND DESIGNING QUERIES AND REPORTS.
CHAPTER 30 THE HTML 5 FORMS PROCESSING. LEARNING OBJECTIVES What the three form elements are How to use the HTML 5 tag to specify a list of words’ form.
TEA/TUG + ALDOT(Mobile) = H(O+I) The TEA/TUG being hosted by ALDOT in Mobile causes Hurricanes to come to Alabama. The TEA/TUG being hosted by ALDOT in.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
PHP (2) – Functions, Arrays, Databases, and sessions.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Python and Web Programming
Guide To UNIX Using Linux Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
Microsoft ® Office Word 2007 Training Mail Merge II: Use the Ribbon and perform a complex mail merge [Your company name] presents:
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
HTML II. Factors to consider in designing a website. Organizing your files. HTML Tables. Unordered Lists. Ordered Lists. HTML Forms. Learning Objectives.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Internet Forms and Database Bob Kisel Amgraf, Inc.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
HTML Hyper Text Markup Language A simple introduction.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Microsoft ® Office Excel 2003 Training Using XML in Excel SynAppSys Educational Services presents:
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Introduction to Unix – CS 21
XP Tutorial 8 Adding Interactivity with ActionScript.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
Web Database Programming Using PHP
Dreamweaver MX Lesson 14: Using Find and Replace.
Data Virtualization Tutorial: Introduction to SQL Script
CHP - 9 File Structures.
Loops BIS1523 – Lecture 10.
Arrays: Checkboxes and Textareas
Web Database Programming Using PHP
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Arrays and files BIS1523 – Lecture 15.
PHP / MySQL Introduction
Intro to PHP & Variables
Teaching slides Chapter 8.
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Web DB Programming: PHP
Presentation transcript:

Chapter 12: Searching in Web applications The first examples use a search form embedded in a Web page to query the deptstore database, which contains the following table. The actual table contains many more records.

Run the products1.cgi program on the Web site. Search for the word rack. Original query hard- coded into returned search form.

Things to note: This search form returns every product containing rack as a substring. The search form was returned with the results so the user can do another search without hitting the back button. The original query is "hardcoded" into the returned search form as an added convenience. By "hardcoded," we mean that the user's original data was coded into the HTML in the value attribute, which gives the text area its initial value when the page loads.

The program is very straight forward. It either prints the initial &search_page or performs a search. ### app logic ################################### if($formHash{"request"} eq "search") { &search; } else { &search_page; }

The initial page contains only the search form, which we print with a helper function. sub search_page { print<<TOP; Search our sports store TOP &searchForm; # no argument --> no hardcoded # value for search field print<<BOTTOM; BOTTOM }

When the search form is submitted, the meat of the &search function is handled by the DBI module and MySQL. sub search { $searchstring = $formHash{"searchstring"}; my $sql = "SELECT item FROM products WHERE item LIKE '%$searchstring%'"; my $qObj = $dbhandle -> prepare($sql) or... $qObj-> execute() or... = (); = $qObj->fetchrow_array()){ $row[0]; } # print the matches in the page # hardcode search string into returned form &searchForm($formHash{"searchstring"});

It is usually desirable to provide refinement options in a search form. SQL's LIKE command does allow minimal pattern matching. LIKE '%z%' # matches contain a z LIKE '%ing' # matches end with ing LIKE '___' # matches any three-character word LIKE '_a_' # matches any three-character word # with an a in the middle Execute products2.cgi on the Web site.

Many database systems also offer the RLIKE (Regular expression LIKE) extension to SQL. SELECT item FROM products WHERE item RLIKE 'regular_expression' Most metacharacters work in RLIKE patterns, but the special location markers and characters classes currently don't: (\A, \Z, \b, \d, \w, etc.) Instead of the beginning and end of string markers, \A and \Z, we have to use ^ and $, respectively. (These also work in Perl patterns.) Further, you can usually manually construct a character class [a-zA-Z0-9_] to use in place of a built-in one. The word class \w, in this case.

So we can implement the following using SQL. SELECT item FROM products WHERE item RLIKE '^rack$' This pattern is equivalent to /\Arack\Z/. The problem is this only matches rack, but not part of a bigger string. For whole word searches, we do want to match something like bicycle rack. We just want to rule out stuff like track shoes.

In Perl, a whole-word search is trivial using word boundary markers: /\brack\b/ Using RLIKE in SQL, we will have to construct our own pattern to replace the unavailable \b. While we're at it, we might as well also match something like rack-o-lamb, which is still in the spirit of whole word matches. That is, a whole-word search will mean that the word stands apart from other words by some non-word character. (^|[^a-zA-Z0-9]) # beginning of string or # not a word character ($|[^a-zA-Z0-9]) # beginning of string or # not a word character

Below is how we implement the whole word search in products2.cgi. $searchstring = $formHash{"searchstring"}; if($formHash{"wholeword"}) { $searchstring = '(^|[^a-zA-Z0-9])'.$searchstring. '($|[^a-zA-Z0-9])'; } If the whole-word search option was chosen (checkbox), we simply concatenate our contrived word boundary pattern on each side of the user's query. my $sql = "SELECT item FROM products WHERE item RLIKE '$searchstring'";

Really, the only other difference between products2.cgi and the first one is that the function which prints the search form needs to know more information to be able to return the search form hardcoded with the original search criteria. That is, when more and more refinement options are added to a search form, the user should get the same form back as a matter of convenience for them.

How can we provide more elaborate search refinements? One option is to allow users to enter regular expressions into search fields. Below, we search for rack or sack. Patterns actually work in our search forms since we pass the user's data straight to the SQL statement. Try some! Most search engines don't feature that capability since regular expressions elude most of the population. One easy way to disable that is simply to pre-process user data, escaping all metacharacters so that they are taken literally. Many do feature search refinements using special words and characters like (and, or, +, -).

The search applications with elaborate search refinements such as seen on the previous slide allow the user simply to manipulate a variety of HTML form elements. Of course, the refinement options must be transformed from submitted form data to database queries. There are several options, depending upon the complexity needed: Construct advanced SQL queries. (This book only features a small subset of SQL). Use the SQL RLIKE command and transform the submitted options into regular expressions usable in SQL. Do relatively simple SQL queries on the database and then further filter the returned records using regular expressions within the Perl program (or whatever language you are using).

There are a couple of common practices regarding Web searches which are fairly easy to implement. The first is secondary processing of the results returned from the database. The second is to limit the number of returned matches to a fixed number per page, and to supply links to deliver the next 10 matches, for example.

See products3.cgi

The secondary processing involves: Testing for the the search string in each returned search match. Using the substitution operator to replace the search string with the search string together with some extra HTML formatting applied. $match =~ s/($searchstring)/ $1 /ig; In this case we simply put the search string in a span container, whose style rule specifies red text. Note that the grouping parentheses capture the search string into the special $1 variable.

Limiting the number of matched results returned per page is significantly more complicated. See products4.cgi The details are somewhat complicated, so you will have to carefully examination the code. The search results are stored in an array, so it boils down to which "chunk" of the array to return. A submitted name=value pair of the form start=11 tells the program where to begin the returned chunk. Then the link for the next ten matches would take the form Next 10 matches A sample run of this program is shown on the next slide.

Sometimes searching in a Web application only entails searching through a site comprised of a bunch of static HTML files. To that end, one first needs to be able to "scan" a directory to obtain the list of files and other directories it contains. Fortunately, that is extremely easy in Perl. opendir(DIRECTORYHANDLE, = readdir(DIRECTORYHANDLE); closedir(DIRECTORYHANDLE); The readdir function returns an array of strings consisting of all the names of the files and directories it finds.

See examples.cgi Goal: Produce a list containing only chapters and examples. Skip any other files or folders.

When we scan the main directory to make the outer list, we "grep out" only those names containing "chapter". opendir(MAINDIR, $mainDir) or = readdir(MAINDIR); = We then loop over the chapter directories, reading in the files from each one. When we scan a given chapter directory, we "grep out" only those names containing "example". foreach $chapdir opendir(DIR, = readdir(DIR); =

When scanning directories, it is often desirable to determine beyond just its name, the nature of each item therein. For example, is each item a file or directory? Below is a sampling of file test operators with a sample usage. $directoryItem; if (-d $directoryItem) { scan the directory to find more files }

Common tasks enabled by the file test operators are constructing site maps or conducting searches through a whole site of static Web pages. The most versatile way to do this is recursively. Below is pseudocode for a recursive search of a Web site comprised of static Web pages. --sub recursiveSearch --Open a directory. --Scan its contents into array. --For each $item --if ( $item is a directory) --call recursiveSearch on $item. (This is the recursive step.) --else $item is a file that we open and search for the user's query

It is instructive to trace how the recursion unfolds given a fairly large directory structure. Go to sitesearch.cgi (fig ) on the Web site and search for "ozzy". You will see which files in this site contain that word.

Putting a whole Web search in a page is very easy. Do a search on the search engine you want to embed in your page. Observe the query string. Search Construct a form in your page which, when submitted, produces an identical query string. It is customary to give the search engine advance credit by putting their icon next to your search utility.

See websearch.html