2016-10-19 6 views
0

eBay APIを使用して5つの結果をキーワードで取得するPHPコードがあります。eBay APIのIFステートメント

結果がある場合にのみ、<h2>List of products</h2>という見出しを表示するためにIF条件を追加します。

// Check to see if the request was successful, else print an error 
if ($resp->ack == "Success") { 
$results = ''; 
// If the response was loaded, parse it and build links 

foreach($resp->searchResult->item as $item) { 
$pic = $item->galleryPlusPictureURL; 
$link = $item->viewItemURL; 
$title = $item->title; 
$price = $item->sellingStatus->currentPrice; 

// For each SearchResultItem node, build a link and append it to $results 
$results .= "<tr><td style=\"text-align: center;\"><img width=\"300\" src=\"$pic\"></td></tr><tr><td><a href=\"$link\" rel=\"nofollow\">$title</a></td></tr><tr><td style=\"text-align: right;\">".$price." &euro;&nbsp;&nbsp;<div><a href=\"$link\" rel=\"nofollow\"><p class=\"bux\">> Offer</p></a></div></td></tr>"; 
} 
} 
// If the response does not indicate 'Success,' print an error 
else { 
$results = "<h3>Oops! The request was not successful. Make sure you are using a valid "; 
$results .= "AppID for the Production environment.</h3>"; 
} 

echo "<table>".$results."</table>"; 

私は<h2>List of products</h2>を示すために、上記のコードを編集するにはどうすればよい少なくとも1件の結果がある場合にのみ、それ以外は何も表示されませんか?

+1

テーブルの一部であるか、テーブルの上(外側)の見出しにしますか? – RST

+0

見出しはテーブルの前に置いてください。 @RST – Maryland

答えて

0
// Check to see if the request was successful, else print an error 
if ($resp->ack == "Success") { 
    $results = ''; 
    // If the response was loaded, parse it and build links 

    foreach($resp->searchResult->item as $item) { 
     $pic = $item->galleryPlusPictureURL; 
     $link = $item->viewItemURL; 
     $title = $item->title; 
     $price = $item->sellingStatus->currentPrice; 

     // For each SearchResultItem node, build a link and append it to $results 
     $results .= "<tr><td style=\"text-align: center;\"><img width=\"300\" src=\"$pic\"></td></tr><tr><td><a href=\"$link\" rel=\"nofollow\">$title</a></td></tr><tr><td style=\"text-align: right;\">".$price." &euro;&nbsp;&nbsp;<div><a href=\"$link\" rel=\"nofollow\"><p class=\"bux\">> Offer</p></a></div></td></tr>"; 
    } 
    if (! empty($results)) 
     $results = '<h2>List of products</h2>'. $results; 
} 
// If the response does not indicate 'Success,' print an error 
else { 
    $results = "<h3>Oops! The request was not successful. Make sure you are using a valid "; 
    $results .= "AppID for the Production environment.</h3>"; 
} 

echo "<table>".$results."</table>"; 
+0

いいえ、このコードを使用すると、「成功」はAPIリクエストが正しいことを意味するため(ただし結果は0になる可能性があるため)、項目が0以上ある場合は常に見出しが表示されます。 – Maryland

+0

コードを調整しました – RST