jogin shar
jogin shar

Reputation: 57

How to show result in json to php

I am trying to show value if main= true then if Offer label Money Line or Total Points or Point Spread then show their outcome values otherwise just show blank 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>

need to show ' no data ' if any offer label main value not true

Result i am getting this is what i am getting Result what i am expecting need to show no data if any offer label main value not true

Upvotes: 0

Views: 80

Answers (2)

Lounis
Lounis

Reputation: 647

I refactored a few the code you provide us, i just use condition if and else to print column empty or with data and according it's parent <th> (e.g Total Points, Money Line, ...). Everything seems to be good, no duplicates, and all <td> go under corresponding <th>. Give a try to this code.

$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>

See the result in picture below.

enter image description here

Upvotes: 1

amamou nesrine
amamou nesrine

Reputation: 13

missing </tbody> and </table>?

Also, if your second "if" expression returns false you won't have <td> but you will have the </td>

Upvotes: 0

Related Questions