Home arrow PHP arrow Page 7 - Using Amazon Web Services With PHP And SOAP (part 1)

Turning The Pages - PHP

It's the coolest store on the Web - and now, its databases are accessible to you. Welcome to Amazon Web Services, an XML-based API that allows you to quickly build an online store that leverages off Amazon.com's massive databases. Find out more, inside.

  1. Using Amazon Web Services With PHP And SOAP (part 1)
  2. Remote Control
  3. The Bare Necessities
  4. Anatomy Class
  5. The Bookworm Turns
  6. Sorting Things Out
  7. Turning The Pages
  8. Weapon Of Choice
  9. Hooking Up
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 103
November 06, 2002

print this article


You'll remember, from my explanation of the various arguments to BrowseNodeSearchRequest() a few pages back, that AWS returns search results in chunks of ten, and the "page" argument must be used to obtain subsequent pages of the result set.

Thus far, all the examples you've seen have been limited to displaying ten items...not very useful in the real world at all. That's why this next example adds previous and next page links to assist in navigating between the different pages of the result set.

<?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); // uncomment the next line to see debug messages // $soapclient->debug_flag = 1; // if no page specified, start with page 1 if (!$_GET['page']) { $page = 1; } else { $page = $_GET['page']; } // create a proxy so that WSDL methods can be accessed directly $proxy = $soapclient->getProxy(); // set up an array containing input parameters to be // passed to the remote procedure $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> Browse the catalog below, or search for a specific title. <p> <table width="100%" border="0" cellspacing="5" cellpadding="0"> <? // parse the $items[] array and extract the necessary information // (image, price, title, author, item URL) 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; ?>">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; ?>">Next page</a> <? } ?> </td> </tr> </table> <font size="-1"> Disclaimer: All product data on this page belongs to Amazon.com. No guarantees are made as to accuracy of prices and information. YMMV! </font> </body> </html>
How does this work? It's actually pretty simple - first, the total number of items in the result set is obtained from the SOAP response and assigned to a variable; this number is then divided by ten and rounded up to obtain the total number of pages to be displayed. Then, previous and next page links are added to the bottom of the page - each link calls the same script again and passes it a new page number via the GET method. This page number is then incorporated into the call to BrowseNodeSearchRequest(), and a new data set is obtained and displayed.

Here's what it looks like:

One caveat, though: AWS 2.0 contains a bug that sometimes causes it to display an incorrect number of total results. Hopefully, this will be fixed in an upcoming release - until then, be warned.

>>> 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: