【问题标题】:Allow user to enter comma separated values into input and return unique items允许用户在输入中输入逗号分隔值并返回唯一项
【发布时间】: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 '&lt;p&gt;' . $arrs[$myValue] . '&lt;/p&gt;'; 不起作用?

编辑 3:使用工作代码更新代码,将每本书的 ISBN 添加到相应的书。

【问题讨论】:

标签: php arrays loops amazon-web-services foreach


【解决方案1】:

$myValue 是一串 ISBN 对吗?

因此,$arrs[$myValue] 将不起作用,因为您需要访问具有整数索引的元素。

如果您将 for 循环更改为使用 $key=>$val 语法,您可以这样做:

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

【讨论】:

  • 嗯没有用。是的,$myValue 存储了所有输入的 ISBN;如果你输入$myValue 而不是$arrs[$index] 你会得到ISBN1, ISBN 2,但$arrs[$index] 什么也不返回。
  • 您是否将 foreach 更改为使用 $index? foreach($similar_products as $index=&gt;$si){
  • 是的,我做到了,仍然没有。
  • 执行var_dump($arrs) 以查看它是否被填充。 $myValue 每个 ISBN 之间是否有逗号和空格?也许改变爆炸使用“,”而不是“,”......?
  • 我收到array(2) { [0]=&gt; string(10) "0345804856" [1]=&gt; string(10) "0307887448" }
【解决方案2】:

这是工作代码:

//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++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多