我有一个JSON API,可以在自来水中返回有关饮料的数据。我对它进行了编码,以便如果$ days_on返回值为1或更小,它会显示该饮料。可以把它想象成网站的“最近点”部分。现在我想做的是,如果本节中没有显示任何饮料,因为在过去的24小时内没有任何饮料,我想要显示一条消息“对不起最近24小时内没有拍过任何东西”。
我在这里的代码完全忽略了我的比较运算符,并且即使在过去24小时内点击了饮料,也始终显示“抱歉......”消息。
<? php
$string = $string = file_get_contents("https://service.digitalpour.com/BeerDashboard/api/v2/MenuItems/5398d80cfb890c0b0cf8baab/1/Tap?apiKey=5398db2afb890c0c88089fe7");
$json_taps = json_decode($string, true);
$t = 1;
foreach($json_taps as $beverage) {
$item_name = $beverage['MenuItemDisplayDetail']['DisplayName'];
$producer_name = $beverage['MenuItemProductDetail']['FullProducerList'];
$beverage_name = $beverage['MenuItemProductDetail']['BeverageNameWithVintage'];
$beverage_color = $beverage['MenuItemProductDetail']['Beverage']['StyleColor'];
$year = $beverage['MenuItemProductDetail']['Year'];
$beverage_abv = $beverage['MenuItemProductDetail']['Beverage']['Abv'];
$beverage_ibu = $beverage['MenuItemProductDetail']['Beverage']['Ibu'];
$beverage_type = $beverage['MenuItemProductDetail']['BeverageType'];
$b_display_order = $beverage['DisplayOrder'];
$producer_location = "";
$producer_url = "";
switch ($beverage_type) {
case "Beer":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Brewery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Brewery']['BreweryUrl'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['BeerStyle']['CultureAwareStyleName'];
break;
case "Cider":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Cidery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Cidery']['CideryUrl'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['CiderStyle']['CultureAwareStyleName'];
break;
case "Mead":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Meadery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Meadery']['MeaderyUrl'];
$beverage_style = "Mead";
break;
case "Wine":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Winery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Winery']['WineryUrl'];
$beverage_style = "Wine";
break;
case "Kombucha":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['KombuchaMaker']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['KombuchaMaker']['Url'];
$beverage_style = "Kombucha";
break;
case "Soft Drink":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkMaker']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkMaker']['Url'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkStyle']['StyleName'];
break;
}
$date_put_on = $beverage['DatePutOn'];
$bottle_size = $beverage['MenuItemProductDetail']['Prices'][0]['Size'];
$bottle_price = $beverage['MenuItemProductDetail']['Prices'][0]['Price'];
$beverage_ps = $beverage['MenuItemProductDetail']['Prices'][0]['DisplayName'];
$in_bottles = $beverage['MenuItemProductDetail']['AvailableInBottles'];
$keg_size = $beverage['MenuItemProductDetail']['KegSize'];
$oz_remaining = $beverage['MenuItemProductDetail']['EstimatedOzLeft'];
$days_on = $beverage['MenuItemProductDetail']['DaysOn'];
$time_on = $beverage['MenuItemProductDetail']['TimeOn'];
$logo = $beverage['MenuItemProductDetail']['Beverage']['BeverageProducer']['LogoImageUrl'];
$growl_logo = "/wp-content/uploads/2014/06/tap-list-logo.png";
$sorry = '<h3>Sorry, nothing new has been tapped in the last 24 hours.<br><a href="/whats-on-tap">Check out our taplist!</a></h3>';
$scale = 1.0; //
//get listing logo
if (empty($logo)) {
$listing_logo = $growl_logo;
}
else {
$listing_logo = $logo;
}
//calculating percentage of keg remaining
// Get Percentage out of 100
if (!empty($keg_size)) {
$percent = $oz_remaining / $keg_size;
}
else {
$percent = 0;
}
// Limit to 100 percent (if more than the max is allowed)
if ($percent > 1) {
$percent = 1;
}
if ($percent < 0) {
$percent = .005;
}
$percent_remaining = number_format($percent * 100, 0);
if ($percent_remaining < 1) {
$percent_remaining = "< 1";
}
elseif($percent_remaining == "100") {
$color = "#00ff00";
$status = "full";
}
elseif($percent_remaining > 75) {
$color = "#00ff00";
$status = "mostly";
}
elseif($percent_remaining > 50) {
$color = "#ffff00";
$status = "half";
}
elseif($percent_remaining > 25) {
$color = "#ffaa00";
$status = "close";
}
else {
$color = "#ff0000";
$status = "dry";
}
$html = '<div class="spb_content_element col-sm-6 column_container listing">'.
'<div class="spb_wrapper">'.
'<div class="container"><div class="row">'.
'<div class="spb_content_element spb_single_image noframe col-xs-4 growl-pic-element mb0">'.
'<div class="spb_wrapper"><figure class="animated-overlay overlay-alt clearfix">'.
'<img width="100" height="225" alt="growler-small" class="attachment-full growl-pic" src="'.$listing_logo.
'"></figure>'.
'</div>'.
'</div>'.
'<div class="spb_content_element col-xs-8">'.
'<div class="spb_wrapper">'.
'<table class="featured-growls">'.
'<tbody>'.
'<tr class="title">'.
'<td class="title">'.$beverage_name.
'</td>'.
'</tr>'.
'<tr class="author">'.
'<td class="by">BY</td><td class="author">'.$producer_name.
'</td>'.
'</tr>'.
'<tr class="from">'.
'<td class="from">'.$producer_location.
'</td>'.
'</tr>'.
'<tr class="details">'.
'<td class="price">percent remaining</td><td class="price_val" style="color:'.$color.
'">'.$percent_remaining.
'%</td>'.
'</tr>'.
'<tr class="details_two">'.
'<td class="type">TYPE</td><td class="type_val">'.$beverage_style.
'</td>'.
'</tr>'.
'<tr class="details_two">'.
'<td class="abv">ABV</td><td class="abv_val">'.$beverage_abv.
'%</td>'.
'<td class="ibus">IBU</td><td class="ibus_val">'.$beverage_ibu.
'</td>'.
'</tr>'.
'</tbody>'.
'</table>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
($t == $countnumrows ? '' : '');
if ($days_on <= 1) {
echo $html;
}
else {
echo $sorry;
break;
}
}; ?>
这是似乎有效的代码。你觉得我的方式有什么问题吗?
<?php
$has_tap = false;
$sorry = '<h3>Sorry, nothing new has been tapped in the last 24 hours.<br><a href="/whats-on-tap">Check out our taplist!</a></h3>';
//all that other stuff
($t == $countnumrows ? '' : '');
if ($days_on <= 1) {
$has_tap = true;
echo $html;
}
}
if ($has_tap == false) {
echo $sorry;
}
?>
答案 0 :(得分:0)
我不确定你想要实现的目标,但是,
我认为您需要移除break;
否则它将在某个时刻退出foreach循环,并且不会遍历整个数组。
试试此代码
<?php
$has_tap = false;
$sorry = '<h3>Sorry, nothing new has been tapped in the last 24 hours.<br><a href="/whats-on-tap">Check out our taplist!</a></h3>';
$string = $string = file_get_contents("https://service.digitalpour.com/BeerDashboard/api/v2/MenuItems/5398d80cfb890c0b0cf8baab/1/Tap?apiKey=5398db2afb890c0c88089fe7");
$json_taps = json_decode($string, true);
$t = 1;
foreach($json_taps as $beverage) {
$item_name = $beverage['MenuItemDisplayDetail']['DisplayName'];
$producer_name = $beverage['MenuItemProductDetail']['FullProducerList'];
$beverage_name = $beverage['MenuItemProductDetail']['BeverageNameWithVintage'];
$beverage_color = $beverage['MenuItemProductDetail']['Beverage']['StyleColor'];
$year = $beverage['MenuItemProductDetail']['Year'];
$beverage_abv = $beverage['MenuItemProductDetail']['Beverage']['Abv'];
$beverage_ibu = $beverage['MenuItemProductDetail']['Beverage']['Ibu'];
$beverage_type = $beverage['MenuItemProductDetail']['BeverageType'];
$b_display_order = $beverage['DisplayOrder'];
$producer_location = "";
$producer_url = "";
switch ($beverage_type) {
case "Beer":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Brewery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Brewery']['BreweryUrl'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['BeerStyle']['CultureAwareStyleName'];
break;
case "Cider":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Cidery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Cidery']['CideryUrl'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['CiderStyle']['CultureAwareStyleName'];
break;
case "Mead":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Meadery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Meadery']['MeaderyUrl'];
$beverage_style = "Mead";
break;
case "Wine":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['Winery']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['Winery']['WineryUrl'];
$beverage_style = "Wine";
break;
case "Kombucha":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['KombuchaMaker']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['KombuchaMaker']['Url'];
$beverage_style = "Kombucha";
break;
case "Soft Drink":
$producer_location = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkMaker']['Location'];
$producer_url = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkMaker']['Url'];
$beverage_style = $beverage['MenuItemProductDetail']['Beverage']['SoftDrinkStyle']['StyleName'];
break;
}
$date_put_on = $beverage['DatePutOn'];
$bottle_size = $beverage['MenuItemProductDetail']['Prices'][0]['Size'];
$bottle_price = $beverage['MenuItemProductDetail']['Prices'][0]['Price'];
$beverage_ps = $beverage['MenuItemProductDetail']['Prices'][0]['DisplayName'];
$in_bottles = $beverage['MenuItemProductDetail']['AvailableInBottles'];
$keg_size = $beverage['MenuItemProductDetail']['KegSize'];
$oz_remaining = $beverage['MenuItemProductDetail']['EstimatedOzLeft'];
$days_on = $beverage['MenuItemProductDetail']['DaysOn'];
$time_on = $beverage['MenuItemProductDetail']['TimeOn'];
$logo = $beverage['MenuItemProductDetail']['Beverage']['BeverageProducer']['LogoImageUrl'];
$growl_logo = "/wp-content/uploads/2014/06/tap-list-logo.png";
$scale = 1.0; //
//get listing logo
if (empty($logo)) {
$listing_logo = $growl_logo;
}
else {
$listing_logo = $logo;
}
//calculating percentage of keg remaining
// Get Percentage out of 100
if (!empty($keg_size)) {
$percent = $oz_remaining / $keg_size;
}
else {
$percent = 0;
}
// Limit to 100 percent (if more than the max is allowed)
if ($percent > 1) {
$percent = 1;
}
if ($percent < 0) {
$percent = .005;
}
$percent_remaining = number_format($percent * 100, 0);
if ($percent_remaining < 1) {
$percent_remaining = "< 1";
}
elseif($percent_remaining == "100") {
$color = "#00ff00";
$status = "full";
}
elseif($percent_remaining > 75) {
$color = "#00ff00";
$status = "mostly";
}
elseif($percent_remaining > 50) {
$color = "#ffff00";
$status = "half";
}
elseif($percent_remaining > 25) {
$color = "#ffaa00";
$status = "close";
}
else {
$color = "#ff0000";
$status = "dry";
}
$html = '<div class="spb_content_element col-sm-6 column_container listing">'.
'<div class="spb_wrapper">'.
'<div class="container"><div class="row">'.
'<div class="spb_content_element spb_single_image noframe col-xs-4 growl-pic-element mb0">'.
'<div class="spb_wrapper"><figure class="animated-overlay overlay-alt clearfix">'.
'<img width="100" height="225" alt="growler-small" class="attachment-full growl-pic" src="'.$listing_logo.
'"></figure>'.
'</div>'.
'</div>'.
'<div class="spb_content_element col-xs-8">'.
'<div class="spb_wrapper">'.
'<table class="featured-growls">'.
'<tbody>'.
'<tr class="title">'.
'<td class="title">'.$beverage_name.
'</td>'.
'</tr>'.
'<tr class="author">'.
'<td class="by">BY</td><td class="author">'.$producer_name.
'</td>'.
'</tr>'.
'<tr class="from">'.
'<td class="from">'.$producer_location.
'</td>'.
'</tr>'.
'<tr class="details">'.
'<td class="price">percent remaining</td><td class="price_val" style="color:'.$color.
'">'.$percent_remaining.
'%</td>'.
'</tr>'.
'<tr class="details_two">'.
'<td class="type">TYPE</td><td class="type_val">'.$beverage_style.
'</td>'.
'</tr>'.
'<tr class="details_two">'.
'<td class="abv">ABV</td><td class="abv_val">'.$beverage_abv.
'%</td>'.
'<td class="ibus">IBU</td><td class="ibus_val">'.$beverage_ibu.
'</td>'.
'</tr>'.
'</tbody>'.
'</table>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
'</div>'.
($t == $countnumrows ? '' : '');
if ($days_on <= 1) {
$has_tap = true;
echo $html;
}
else {
echo $sorry;
break;
}
}
if($has_tap === false){
echo $sorry;
}
?>