I am using PHP with Amazon's Product Advertising API to have the user enter an ISBN number into a search form and have it return the product info.
Is there a way to allow the user to enter multiple ISBN numbers, separated by a comma, and return the product info for each number entered?
Here is what I have so far that works:
//allow users to enter multiple ISBN, separated by a comma
$myValue = $_POST['isbnNum'];
$arrs = explode(", ", $myValue);
$similar = array(
'Operation' => 'ItemLookup',
'IdType' => 'ISBN',
'ItemId' => $myValue,
'SearchIndex' => 'Books',
'ResponseGroup' => 'Medium'
);
$result = $amazon->queryAmazon($similar);
$similar_products = $result->Items->Item;
//arrays
$aws_items = array();
//array counter
$i = 0;
foreach($similar_products as $si){
$item_url = $si->DetailPageURL; //get its amazon url
$img = $si->MediumImage->URL; //get the image url
$title = $si->ItemAttributes->Title; //product title
$isbn = $si->ItemAttributes->ISBN; //product title
$price = $si->OfferSummary->LowestNewPrice->FormattedPrice; //product price
$weight = $si->ItemAttributes->ItemDimensions->Weight; //product weight
//item array
$item = [];
$item['title'] = $title;
$item['price'] = $price;
$item['weight'] = $weight;
$item['isbn'] = $isbn;
//add decimal point two spaces from right of product dimensions
$length = number_format(($si->ItemAttributes->ItemDimensions->Length/100),2); //item length
$width = number_format(($si->ItemAttributes->ItemDimensions->Width/100),2); //item width
$height = number_format(($si->ItemAttributes->ItemDimensions->Height/100),2); //item height
//print HTML
echo '<div class="isbn-item">';
echo '<img src="' . $img . '" />';
echo '<h2>' . $title . '</h2>';
echo '<p>' . $item['isbn'] . '</p>'; //isbn
/*echo '<p>' . $si->ItemAttributes->ListPrice->FormattedPrice . '</p>'; //item price*/
echo '<p>' . $item['price'] . '</p>';
echo '<p>' . $length . ' x ' . $width . ' x ' . $height . ' inches</p>';
echo '<p>' . $weight . ' ounces</p>';
echo '<input name="submit" type="submit" value="Add to Cart">';
echo '</div>';
//Increment the counter by 1
$i++;
}
Edit: So I added the the explode part at the top, and now I am returning multiple items. However, in the HTML, since I am outputting $myValue
, each item is getting all the ISBN values entered, instead of the one that are associated with them. I understand I need to access the $arrs
array (right?), but I'm no sure where/how.
Edit 2: Why wouldn't echo '<p>' . $arrs[$myValue] . '</p>';
work?
Edit 3: Updated code with working code that adds each books ISBN to the corresponding book.