Jump to content

how to use the boolean in php with json response code


livetrainstatus
Go to solution Solved by Muddy_Funster,

Recommended Posts

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 else it will display the not arrived message in the foreach loop .

 

 

i have decoded and stored the decoded values in the $json variable
 
Kindly help me to suggest the idea and help me to solve the issue
 
my php code:
<!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/jvtnb8382/';
    $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>'.$stop['has_departed'].'<TD>'.$stop['latemin']."\n";
    }
  } else {
    echo "Something went wrong, please notify to admin [support@livetrainrunningstatus.co.in]";
  }
?>
    </TABLE>
  </BODY>
</HTML>

 

Link to comment
Share on other sites

You would use an if conditional check on the value of the has_arrived element in the array and present an output depending on the content. A shorthand way of doing this is with a ternary operator

 

...
foreach...
($stop['has_arrived'] === true) ? $stop['has_arrived'] = "Arrived" : $stop['has_arrived'] = "Still not here yet!";
echo ...
...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.