Book Store Web Service - Eecs.csuohio.edu

Transcription

Book Store Web ServiceWeb Page home page:

Searching by category with two characters (ajax request is made when the search fieldcount reaches 3 characters): (NOTE* I am using ajax request to prevent from page refresh, sowhen they user types in a search criteria parameter an ajax request is made to a php script thatbuilds a query based on the parameter to search the database by category column or titlecolumn.)(as you can see above, nothing will display when there are less than 3 characters in the searchfield)

Searching by category with three characters:(As you can see 3 books were returned which has CHILDREN as the category)

Searching by title:(Note the search parameter does not have to be the entire book title, it can be partial book titleor category)

Searching by book title example 2:

Searching by category example 2:

Clearing the search field:(note clearing the field will remove the previous search results so the user is not confused withwhat results belong to what search criteria)

Extracted XML values into database table:

XML Parsing and data extraction Script:(Note* I have created a pre.php file which is used for auto loading my DB connection and I havea created a DB.php file which performs the mysql db connection calls)pre.php file contents: ?phpdate default timezone set('America/New York');function my autoloader( class name) { root ini get('doc root');strlen( root) 1 ? root SERVER['DOCUMENT ROOT'] : root root;if(file exists(" root/class/ class name.php")) {include " root/class/ class name".'.php';}}spl autoload register('my autoloader'); INPUT array();if(isset( GET)){foreach( GET as key val) { INPUT[ key] val;}}if(isset( REQUEST)){foreach( REQUEST as key val){ INPUT[ key] val;}}if(isset( POST)){foreach( POST as key val) { INPUT[ key] val;}}? DB.php file contents:

?phpclass DB extends mysqli {function construct( db name) {parent:: construct("localhost", "root", "root", db name);if (mysqli connect error()) {die('Connect Error (' . mysqli connect errno() . ') '. mysqli connect error());}}}

Xml parser file contents: ?phpinclude once 'pre.php'; db new DB('bookstore'); createSql "CREATE TABLE books (id INT(11) AUTO INCREMENT PRIMARY KEY,category VARCHAR(20) NOT NULL,title VARCHAR(100) NOT NULL,author VARCHAR(100) NOT NULL,year YEAR NOT NULL,price DOUBLE NOT NULL,image link VARCHAR(50) NULL,image BLOB NULL)";if ( db- query("SHOW TABLES LIKE 'books'")- num rows 0) {if ( db- query( createSql) ! TRUE) {die("Error creating table");}} xml simplexml load file("bookstore.xml") or die("Error: Cannot create object");foreach ( xml as book) { fp fopen( book- image, "rb"); blobdata fread( fp, filesize( book- image)); sql "INSERT INTO books ( category , title , author , year , price , image link , image )VALUES ('{ book- attributes()[0]}','{ db- real escape string( book- title)}','{ db real escape string( book- author)}', book- year, book- price, ' book- image', '{ db- real escape string( blobdata)}')";if ( db- query( sql) ! TRUE) {die("Error Inserting record into table: ". sql);}}

Search Web Page File Content: ?phpinclude once 'pre.php'; ? !DOCTYPE html html xmlns "http://www.w3.org/1999/xhtml" lang "en" xml:lang "en" head meta charset "utf-8" meta http-equiv "X-UA-Compatible" content "IE edge" meta name "viewport" content "width device-width, initial-scale 1" title Books-R-Us Search /title !-- Latest compiled and minified CSS -- link rel "stylesheet"href ss/bootstrap.min.css"integrity "sha3841q8mTJOASx8j1Au rossorigin "anonymous" !--[if lt IE 9] script src min.js" /script script src js" /script ![endif]-- /head body role "document" nav class "navbar navbar-inverse navbar-fixed-top" div class "container" div class "navbar-header" a class "navbar-brand" href "#" Books-R-Us /a /div /div /nav br br br br div class "container theme-showcase" role "main" div class "row" div class "jumbotron" div class "col-lg-6 col-md-offset-3" input type "text" id "search val" class "form-control" placeholder "Book nameor category." /div !-- /.col-lg-6 -- /div /div div class "row"

div id "results" /div /div /div !-- Latest compiled and minified JavaScript -- !-- jQuery (necessary for Bootstrap's JavaScript plugins) -- script src .3/jquery.min.js" /script script src s/bootstrap.min.js"integrity ss1yVqOtnepnHVP9aJ7xS"crossorigin "anonymous" /script script type "text/javascript" ("#search val").keyup(function() {if ( .trim( ("#search val").val()).length 2) {search();}});function search() { ("#results").html('');var html " div class 'row' div class 'col-lg-10 col-md-offset-1' h3 SearchResults /h3 ";var searchParameter .trim( ("#search val").val()); .ajax({type: "GET",url: "api/",data: {task: "search",param: searchParameter},success: function(data){var results .parseJSON(data);if (results.length 0) { ("#results").html(" h2 No results found. /h2 ");return;} .each(results, function( key, book ) {html " div class 'col-sm-6 col-md-4' ";html " div class 'thumbnail' ";html " img src '" book.image url "' alt '" book.title "' ";html " div class 'caption' h3 class 'text-center' Book Information /h3 ";html " p class 'lead' Title: " book.title " /p ";html " p class 'lead' Author: " book.author " /p ";html " p class 'lead' Year: " book.year " /p ";

html " h2 class 'help-block text-center' " book.price " /h2 ";html " /div /div /div ";});html " /div /div "; ("#results").html(html);}});} /script /body /html

Query Processing script (api/index.php - ajax calls this script) ?phpinclude once './pre.php'; db new DB("bookstore");switch ( INPUT['task']) {case "search": data array(); param db- real escape string(strtoupper( INPUT['param'])); sql "SELECT * FROM books WHERE (category like '{ param}%') OR (title like'{ param}%') order by title asc"; result db- query( sql);if (! result) {echo "no results";echo json encode( data);}while ( row result- fetch object()) { data[] array('id' row- id, 'category' row- category,'title' row- title, 'author' row- author,'year' row- year, 'price' row- price, 'image url' row- image link);}echo json encode( data);break;default:echo "select a task";break;}

Bookstore XML file ?xml version "1.0"? bookstore book category "COOKING" title lang "en" Everyday Italian /title author Giada De Laurentiis /author year 2005 /year price 30.00 /price image img/ItalianCookingBook.jpg /image /book book category "CHILDREN" title lang "en" Harry Potter and the Chamber Of Secrets /title author J K. Rowling /author year 2005 /year price 49.99 /price image img/HarryPotterChamberOfSecretes.jpg /image /book book category "CHILDREN" title lang "en" Harry Potter and the Cursed Child /title author J K. Rowling /author year 2005 /year price 29.99 /price image img/HarryPotterCursedChild.jpg /image /book book category "CHILDREN" title lang "en" Harry Potter and the Sorcerer's Stone /title author J K. Rowling /author year 2005 /year price 39.99 /price image img/HarryPotterSorcererStone.jpg /image /book book category "WEB" title lang "en" Learning XML /title author Erik T. Ray /author year 2011 /year price 39.95 /price image img/XMLBook.jpg /image /book /bookstore

Searching by category with two characters (ajax request is made when the search field count reaches 3 characters): (NOTE* I am using ajax request to prevent from page refresh, so when they user types in a search criteria parameter an ajax request is made to a php script that