我正在使用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添加到相应的书籍中。
答案 0 :(得分:0)
$myValue
是一串ISBN吗?
因此,$arrs[$myValue]
将无效,因为您需要使用整数索引访问元素。
如果您更改for循环以使用$ key =&gt; $ val语法,则可以执行以下操作:
foreach($similar_products as $index=>$si){
...
echo '<p>' . $arrs[$index] . '</p>'; //isbn
...
}
答案 1 :(得分:0)
这是工作代码:
//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++;
}