Throughout this Short Cut, we use the same example to illustrate Ajax concepts. This approach best demonstrates the various ways of accomplishing the same task while staying with an example you may find familiar.
Our applications allow the user to type in a full or partial title or author of a book and then present information about any matching books and Short Cuts.
The use case for the example is as follows:
User visits the page and is presented with a form that has the fields "Title" and "Author."
User types in the partial title of the book and/or the partial author name.
The user presses "Search."
The result div (box) is filled with the text "Performing search... please wait."
The server is queried using an XMLHTTPRequest object for the appropriate books.
The server returns the results using a specified data format (XML or JSON).
The results are displayed to the user.
Throughout this Short Cut the result will stay the same but the methods used to obtain that result will gradually become more sophisticated.
Because this Short Cut covers Ajax and PHP only, we won't discuss database access and searching algorithms. Instead, we use a simple Comma Separated Values (CSV) file to store the books (and Short Cuts!) for which we will be searching and a basic class to load and search the file.
This CSV file and class is used in every subsequent example. You can, of course, modify the class to search in a more expansive database—be creative. First copy this data into a file in your web server's document root called books.csv:
Ajax with PHP 5, Andrew Curioso,2007,O'Reilly Media Google Web Toolkit for Ajax, Bruce W. Perry,2007,O'Reilly Media Learning JavaScript, Shelly Powers,2006,O'Reilly Media Ajax Hacks, Bruce W. Perry,2006,O'Reilly Media Object-Oriented PHP, Peter Lavin,2006,O'Reilly Media PHP Hacks, Jack Herrington,2005,O'Reilly Media Essential PHP Security, Chris Shiflett,2005,O'Reilly Media PHP in a Nutshell, Paul Hudson,2005,O'Reilly Media Learning PHP 5,David Sklar,2004,O'Reilly Media
The file contains four columns. The first column is the name of the book, followed by the author, the publication year, and the publisher. Feel free to add and delete books from the file as you see fit. If any of your columns contain a comma, be sure to enclose the entire column in quotes.
We now need a way to load and search the file we just created. To do this we make a BookLoader class. Copy the class below into a file BookLoader.class.php. The details of how this class works is outside the scope of Ajax; however, feel free to modify it as you see fit as long as you keep the class name and method name the same.
<?php class BookLoader { public static function Search($title,$author,$f="books.csv") { $fields = array("Title","Author","Year","Publisher"); $results = array(); $h = fopen($f, 'r'), $title = trim($title); $author = trim($author); while ( ($line = fgetcsv($h, 1000)) !== FALSE) { if ( count($line) != 4 ) continue; if ( (@strpos($line[0],$title) !== false || $title=="") && (@strpos($line[1],$author) !== false || $author=="") ) { $results[] = array_combine($fields,$line); } } fclose($h); return $results; } } ?>
The class BookLoader uses the function
array_combine()
and static methods, neither of
which are available in PHP 4. The array_combine()
function takes two arrays. The first array will be used for the keys
and the second array will be used as the values. This can be emulated
easily in PHP 4.
The naming of files in PHP is not an exact science. The examples in this Short Cut use a simple naming convention that is very commonly used throughout the PHP community. If the file is a class, we will name the file ClassName.class.php, where class is the word "class" and ClassName should be replaced by the name of the class (like we previously did with BookLoader.class.php). On the other hand, if the file is not a class we simply give it a name that makes sense given what the file does. For example, the helloworld.php file from the previous section was named after the example.
This is the first example of a utility class. Utility classes typically do not have any dynamic (nonstatic) member methods (functions) or properties (variables). Because the class only has one member and that member is static, you can access it without initializing the class. To search for this Short Cut in the CSV file, you use just one line of code:
$books = BookLoader::Search(title, author);
The third parameter is defaulted to "book.csv" so we do not have to specify it unless we are using a different CSV file than this one. The return value is an array of all the books that were found.
34.231.180.210