Home arrow PHP arrow Page 3 - Using Amazon Web Services With PHP And SOAP (part 2)

Searching For Words - PHP

In this concluding article, find out how to add search features to your AWS-backed online store, and link your product pages up to Amazon.com's shopping carts and wish lists.

  1. Using Amazon Web Services With PHP And SOAP (part 2)
  2. Rolling The Stones
  3. Searching For Words
  4. Drilling Deeper
  5. Riding The Popularity Metrics
  6. Bagging It
  7. Linking Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 21
November 13, 2002

print this article


You might remember this page from the first part of this article:

You might also remember that I never got around to adding a search box. Let's do that now:

<?php // include class include("nusoap.php"); // create a instance of the SOAP client object $soapclient = new soapclient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl", true); // create a proxy so that WSDL methods can be accessed directly $proxy = $soapclient->getProxy(); // if no page specified, start with page 1 if (!$_GET['page']) { $page = 1; } else { $page = $_GET['page']; } // if keyword available, use it if ($_POST['keyword'] && $_POST['keyword'] != "") { $keyword = $_POST['keyword']; } if ($_GET['keyword'] && $_GET['keyword'] != "") { $keyword = $_GET['keyword']; } // if a keyword is present, do a keyword search if ($keyword) { $params = array( 'keyword' => htmlentities($keyword), 'page' => $page, 'mode' => 'books', 'tag' => 'melonfire-20', 'sort' => '+pmrank', 'type' => 'lite', 'devtag' => 'YOUR-TOKEN-HERE' ); // invoke the method $result = $proxy->KeywordSearchRequest($params); } else { // else perform a node browse $params = array( 'browse_node' => 18, 'page' => $page, 'mode' => 'books', 'tag' => 'melonfire-20', 'type' => 'lite', 'devtag' => 'YOUR-TOKEN-HERE' ); // invoke the method $result = $proxy->BrowseNodeSearchRequest($params); } $total = $result['TotalResults']; $items = $result['Details']; // format and display the results ?> <html> <head> <basefont face="Verdana"> </head> <body bgcolor="white"> <p>&nbsp;<p> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="Navy"><font color="white" size="-1"><b>Welcome to The Mystery Bookstore!</b></font></td> <td bgcolor="Navy" align="right"><font color="white" size="-1"><b><? echo date("d M Y", mktime());?></b></font></td> </tr> </table> <p> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td>Browse the catalog below, or search for a specific title &nbsp;</td> <td><br><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>"><input type="text" name="keyword"></form></td> </tr> </table> <p> <table width="100%" border="0" cellspacing="5" cellpadding="0"> <? foreach ($items as $i) { ?> <tr> <td align="center" valign="top" rowspan="3"><a href="<? echo $i['Url']; ?>"><img border="0" src=<? echo $i['ImageUrlSmall']; ?>></a></td> <td><font size="-1"><b><? echo $i['ProductName']; ?></b> / <? echo implode(", ", $i['Authors']); ?></font></td> </tr> <tr> <td align="left" valign="top"><font size="-1">List Price: <? echo $i['ListPrice']; ?> / Amazon.com Price: <? echo $i['OurPrice']; ?></font></td> </tr> <tr> <td align="left" valign="top"><font size="-1"><a href="<? echo $i['Url']; ?>">Read more about this title on Amazon.com</a></font></td> </tr> <tr> <td colspan=2>&nbsp;</td> </tr> <? } ?> </table> <!-- next and prev page links --> <? $pageCount = ceil($total/10); ?> <table width="100%" cellspacing="0" cellpadding="5"> <tr> <td align="left"> <? if ($page != 1) { ?> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=<? echo $page-1; ?>&keyword=<? echo $keyword; ?>">Previous page</a> <? } ?> &nbsp; </td> <td align="center">Page <? echo $page; ?> of <? echo $pageCount; ?></td> <td align="right"> &nbsp; <? if ($page < $pageCount) { ?> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=<? echo $page+1; ?>&keyword=<? echo $keyword; ?>">Next page</a> <? } ?> </td> </tr> </table> </body> </html>
As you can see, this is pretty simple - I've added a small input box for the user to enter a search term, and modified the form processor to check for a keyword and run a KeywordSearchRequest() if it's present, or a BrowseNodeSearchRequest() for the default node if it isn't. The next and previous page links also now pass an additional "keyword" parameter back and forth, so that the user can page through either the product catalog returned by BrowseNodeSearchRequest() or the result set returned by KeywordSearchRequest().

Here are a couple of screenshots demonstrating what the final result looks like:

>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: