[英]How to show result in json to php
我試圖顯示價值如果 main= true 那么如果提供標簽 Money Line 或 Total Points 或 Point Spread 然后顯示它們的結果值,否則只顯示空白 td
$url = 'https://iakshay.website/test/demo.json';
$data = file_get_contents($url);
$characters = json_decode($data, true); ?>
<table>
<tbody>
<tr>
<th>Date/Time</th>
<th>Games</th>
<th>Spread</th>
<th>Total</th>
<th>Moneyline</th>
</tr>
<?php foreach ($characters['events'] as $events){
$str=$events['startDate'];
$actdate=explode("T",$str);
<td>".$actdate[0]."</td>";
<td>".$events['name']."</td>";
foreach ($events['offers'] as $offers){
if($offers['main']==true || $offers['main']==1){
if($offers['label']=="Point Spread"){ ?>
<td> <?php echo $offers['outcomes'][0]['line'].'('.$offers['outcomes'][0]['oddsAmerican'].')<br/>'. $offers['outcomes'][1]['line'].'('.$offers['outcomes'][1]['oddsAmerican'];
} else {
echo '<td> </td>';
}?></td>
<?php
if($offers['label']=="Money line"){ ?>
<td> <?php echo $offers['outcomes'][0]['line'].'('.$offers['outcomes'][0]['oddsAmerican'].')<br/>'. $offers['outcomes'][1]['line'].'('.$offers['outcomes'][1]['oddsAmerican'];
} else {
echo '<td> </td>';
}?></td>
<?php
if($offers['label']=="Total line"){ ?>
<td> <?php echo $offers['outcomes'][0]['line'].'('.$offers['outcomes'][0]['oddsAmerican'].')<br/>'. $offers['outcomes'][1]['line'].'('.$offers['outcomes'][1]['oddsAmerican'];
} else {
echo '<td> </td>';
}?></td>
<?php
}
echo '<tr>';
}
}?>
</table>
如果任何報價標簽主值不正確,則需要顯示“無數據”
結果我得到這就是我得到的結果如果任何報價標簽主值不正確,我所期望的不需要顯示任何數據
我重構了一些您提供給我們的代碼,我只是使用條件if
和else
來打印列為空或帶有數據並根據它的父<th>
(例如 Total Points、Money Line...)。 一切似乎都很好,沒有重復,所有<td>
都在相應的<th>
。 試試這個代碼。
$url = 'https://iakshay.website/test/demo.json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
?>
<table>
<tbody>
<tr>
<th>Date/Time</th>
<th>Games</th>
<th>Spread</th>
<th>Total</th>
<th>Moneyline</th>
</tr>
<?php foreach ($characters['events'] as $events) {
$str = $events['startDate'];
$actdate = explode("T", $str);
$row = '';
foreach ($events['offers'] as $offers) {
if (isset($offers['main']) && $offers['main']) {
$row .= '<tr><td>' . $actdate[0] . '</td><td>' . $events["name"] . '</td>';
if (\strcasecmp($offers['label'], "Point Spread") === 0) {
$line = $offers['outcomes'][0]['label'] ?? '';
$oddsAmerican = $offers['outcomes'][0]['oddsAmerican'] ?? '';
$line2 = $offers['outcomes'][1]['label'] ?? 'No Data';
$oddsAmerican2 = $offers['outcomes'][1]['oddsAmerican'] ?? '';
$row .= "<td>$line ($oddsAmerican) </br> $line2 ($oddsAmerican2)</td>";
} else {
$row .= '<td>NO DATA</td>';
}
if (\strcasecmp($offers['label'], "Total Points") === 0) {
$line = $offers['outcomes'][0]['label'] ?? '';
$oddsAmerican = $offers['outcomes'][0]['oddsAmerican'] ?? '';
$line2 = $offers['outcomes'][1]['label'] ?? '';
$oddsAmerican2 = $offers['outcomes'][1]['oddsAmerican'] ?? '';
$row .= "<td>$line ($oddsAmerican) </br> $line2 ($oddsAmerican2)</td>";
} else {
$row .= '<td>NO DATA</td>';
}
if (\strcasecmp($offers['label'], "Money line") === 0) {
$line = $offers['outcomes'][0]['label'] ?? '';
$oddsAmerican = $offers['outcomes'][0]['oddsAmerican'] ?? '';
$line2 = $offers['outcomes'][1]['label'] ?? '';
$oddsAmerican2 = $offers['outcomes'][1]['oddsAmerican'] ?? '';
$row .= "<td>$line ($oddsAmerican) </br> $line2 ($oddsAmerican2)</td>";
} else {
$row .= '<td>NO DATA</td>';
}
$row .= '</tr>';
echo $row;
}
}
}
?>
</table>
見下圖的結果。
缺少</tbody>
和</table>
?
此外,如果您的第二個“if”表達式返回 false,您將沒有<td>
但您將擁有</td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.