13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

828 Chapter 33 Connecting to <strong>Web</strong> Services with XML <strong>and</strong> SOAP<br />

Listing 33.6 showBrowseNode() Function from bookdisplayfunctions.php—A<br />

List of Categories<br />

// For a particular browsenode, display a page of products<br />

function showBrowseNode($browseNode, $page, $mode) {<br />

$ars = getARS('browse', array('browsenode'=>$browseNode,<br />

'page' => $page, 'mode'=>$mode));<br />

showSummary($ars->products(), $page, $ars->totalResults(),<br />

$mode, $browseNode);<br />

The showBrowseNode() function does exactly two things. First, it calls the getARS()<br />

function from cachefunctions.php.This function gets <strong>and</strong> returns an<br />

AmazonResultSet object (more on this in a moment).Then it calls the showSummary()<br />

function from bookdisplayfunctions.php to display the retrieved information.<br />

The getARS() function is absolutely key to driving the whole application. If you<br />

work your way through the code for the other actions—viewing details, images, <strong>and</strong><br />

searching—you will find that it all comes back to this.<br />

Getting an AmazonResultSet Class<br />

Let’s look at that getARS() function in more detail. It is shown in Listing 33.7.<br />

Listing 33.7 getARS() Function from cachefunctions.php—A Resultset for a<br />

Query<br />

// Get an AmazonResultSet either from cache or a live query<br />

// If a live query add it to the cache<br />

function getARS($type, $parameters) {<br />

$cache = cached($type, $parameters);<br />

if ($cache) {<br />

// if found in cache<br />

return $cache;<br />

} else {<br />

$ars = new AmazonResultSet;<br />

if($type == 'asin') {<br />

$ars->ASINSearch(padASIN($parameters['asin']), $parameters['mode']);<br />

}<br />

if($type == 'browse') {<br />

$ars->browseNodeSearch($parameters['browsenode'],<br />

$parameters['page'], $parameters['mode']);<br />

}<br />

if($type == 'search') {<br />

$ars->keywordSearch($parameters['search'], $parameters['page'],<br />

$parameters['mode']);<br />

}<br />

cache($type, $parameters, $ars);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!