Page 1 of 1

how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 12:14 am
by sathya
I want to make a foreach loop to display the has_arrived status in php.
when i tried with var_dump(has_arrived) it returned the output as false.but i want the output as,

when the has_arrived status is true it should display the arrived text message instead else it will display the not arrived message in the foreach loop .

Kindly help me to suggest the idea and help me to solve the issue



JSON Response:

Code: Select all

{
"route": [
        {
            "no": 1,
            "station": "CDG",
            "has_arrived": false,
            "has_departed": true,
            "day": 0,
            "distance": 0,
            "scharr": "Source",
            "schdep": "12:00",
            "actarr": "00:00",
            "actdep": "12:00",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 0
        }
 {
            "no": 2,
            "station": "UMB",
            "has_arrived": true,
            "has_departed": true,
            "day": 0,
            "distance": 67,
            "scharr": "12:40",
            "schdep": "12:42",
            "actarr": "12:40",
            "actdep": "12:42",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 0
        }
]
}
Thanks

Re: how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 1:08 am
by requinix
What's your PHP code? Have you already decoded the JSON into an object or array?

Re: how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 1:55 am
by sathya
yes i have decoded the json and stored the decoded values in $json variable.

Re: how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 3:11 am
by requinix
Good. So how about posting that code?

Re: how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 3:29 am
by sathya
The below code is my php code:

Code: Select all

<!DOCTYPE html>
<HTML>
  <HEAD>
    <META charset="utf-8">
    <TITLE>Train Status Time Table</TITLE>
  </HEAD>
  <BODY>
    <TABLE>
      <TR><TH>Station<TH>Scheduled<TH>Actual<TH>Status/Delay
<?php
  $json = '';
  if(isset($_GET['trainnumber']) && (isset($_GET['doj']))) { 
    $url = 'http://api.railwayapi.com/live/train/'.$_GET['trainnumber'].'/doj/'.$_GET['doj'].'/apikey/*/';
    $json = json_decode(file_get_contents($url), true);
    foreach($json['route'] as $stop) {
      echo '      <TR><TD>'.$stop['station'].'<TD>'.$stop['scharr'].'<TD>'.$stop['actarr'].'<TD>'.$stop['has_arrived'].'<TD>"\n";
    }
  } else {
    echo "Something went wrong, please notify to admin [support@livetrainrunningstatus.co.in]";
  }
?>
    </TABLE>
  </BODY>
</HTML>

Here i need the boolean json response to be added in for each loop and bring the result for false as arrived and true as not arrived .

Thanks

Re: how to use the boolean in php with json response code

Posted: Wed Dec 30, 2015 4:05 am
by requinix
Okay, well, $stop['has_arrived'] is true or false, so use it in an if to decide which message you want to show.