how to use the boolean in php with json response code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

how to use the boolean in php with json response code

Post 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
Last edited by requinix on Wed Dec 30, 2015 1:05 am, edited 1 time in total.
Reason: please use [syntax=javascript] tags when posting JSON; added outer {}s for highlighting
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

What's your PHP code? Have you already decoded the JSON into an object or array?
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

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

Post by sathya »

yes i have decoded the json and stored the decoded values in $json variable.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Good. So how about posting that code?
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

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

Post 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
Last edited by requinix on Wed Feb 03, 2016 11:37 pm, edited 1 time in total.
Reason: removing api key by request
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply