livetrainstatus Posted December 30, 2015 Share Posted December 30, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/300054-how-to-use-the-boolean-in-php-with-json-response-code/ Share on other sites More sharing options...
Muddy_Funster Posted December 30, 2015 Share Posted December 30, 2015 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 ... ... Quote Link to comment https://forums.phpfreaks.com/topic/300054-how-to-use-the-boolean-in-php-with-json-response-code/#findComment-1528830 Share on other sites More sharing options...
livetrainstatus Posted December 30, 2015 Author Share Posted December 30, 2015 thank you so much Muddy_Funster i got understood the concept now by using the ternary operator..i dont know how to apply before u said..now captured the things.thank u so much..many thanks Quote Link to comment https://forums.phpfreaks.com/topic/300054-how-to-use-the-boolean-in-php-with-json-response-code/#findComment-1528855 Share on other sites More sharing options...
Solution Muddy_Funster Posted January 4, 2016 Solution Share Posted January 4, 2016 You are very welcome, best of luck with the rest of your project. If you are done could you mark the topic as answered please? Quote Link to comment https://forums.phpfreaks.com/topic/300054-how-to-use-the-boolean-in-php-with-json-response-code/#findComment-1529071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.