dsbgltg159136540 2016-04-21 21:44
浏览 22

允许用户在输入中输入逗号分隔值并返回唯一项

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.

  • 写回答

2条回答 默认 最新

  • dougou8458 2016-04-22 01:29
    关注

    $myValue is a string of ISBNs right?

    So, $arrs[$myValue] won't work because because you need to access the elements with an integer index.

    If you change your for loop to use the $key=>$val syntax, you can do this:

    foreach($similar_products as $index=>$si){   
       ...
            echo '<p>' . $arrs[$index] . '</p>';    //isbn
       ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看