【发布时间】:2016-04-21 21:44:32
【问题描述】:
我正在将 PHP 与亚马逊的产品广告 API 结合使用,让用户在搜索表单中输入 ISBN 号并让它返回产品信息。
有没有办法让用户输入多个 ISBN 号码,用逗号分隔,并返回每个输入号码的产品信息?
这是我目前所拥有的:
//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++;
}
编辑:所以我在顶部添加了爆炸部分,现在我返回了多个项目。但是,在 HTML 中,由于我输出的是 $myValue,因此每个项目都输入了所有 ISBN 值,而不是与它们关联的值。我知道我需要访问 $arrs 数组(对吗?),但我不确定在哪里/如何。
编辑 2:为什么echo '<p>' . $arrs[$myValue] . '</p>'; 不起作用?
编辑 3:使用工作代码更新代码,将每本书的 ISBN 添加到相应的书。
【问题讨论】:
-
是的。以逗号展开,查找每个单独的值,然后向用户报告。
-
@MarcB 谢谢,我让它工作了。每个值将返回一个结果;关于如何将每个值显示到其各自结果的任何想法?
标签: php arrays loops amazon-web-services foreach