/    Sign up×
Community /Pin to ProfileBookmark

how to pass and iterate the value from foreach loop to api url and convert to echo ?

Hi ,i have posted my code here.

Livetrainstatus.php code:

[code=php]<!DOCTYPE html>
<HTML>
<HEAD>
</head>
<body>
<?php
$json = ”;
if(isset($_GET[‘trainnumber’]) && (isset($_GET[‘doj’]))) //passing the train number and doj from home page to here

{
$url = ‘http://api.railwayapi.com/live/train/’.$_GET[‘trainnumber’].’/doj/’.$_GET[‘doj’].’/apikey/jvtnb8382/’; //api url
$json = json_decode(file_get_contents($url), true);
echo “<center>Station<center><center>Scheduled<center>center>Actual<center><center>Status/Delay”;
foreach($json[‘route’] as $stop) {

($stop[‘has_arrived’] === true) ? $stop[‘has_arrived’] = “Arrived” : $stop[‘has_arrived’] = “Still not here yet!”;

echo ‘ <center>’.$stop[‘station’].'<center>’.$stop[‘scharr’].'<center>’.$stop[‘actarr’].'<center>’.$stop[‘has_arrived’].”n”;
}
} else {
echo “Something went wrong, please notify to admin [[email protected]]”;
}
?>
</BODY>
</HTML>
[/code]

output:

[QUOTE]

here it will produce the station,scheduled arrival,actual arrival,arrived status.

[/QUOTE]

Json Response:

[QUOTE]

{
“response_code”: 200,
“stations”: [
{
“lng”: 83.3731675,
“lat”: 26.7605545,
“code”: “GKP”,
“state”: “Uttar Pradesh”,
“fullname”: “Gorakhpur Jn”
},
{
“lng”: 83.5842775,
“lat”: 26.6492695,
“code”: “CC”,
“state”: “Uttar Pradesh”,
“fullname”: “Chauri Chaura”
},
{
“lng”: 83.2142269,
“lat”: 26.758544,
“code”: “SWA”,
“state”: “Uttar Pradesh”,
“fullname”: “Sahjanwa”
},
{
“lng”: 83.3742644,
“lat”: 26.7401226,
“code”: “DDP”,
“state”: “Uttar Pradesh”,
“fullname”: “Daudpur”
}
]
}

[/QUOTE]

This is the url format for getting the above api response.

[QUOTE]

[url]http://api.railwayapi.com/code_to_name/code/gkp/apikey/12345/[/url]

[/QUOTE]

i want to pass all the station name from livetrainstatus.php to this url and make the output as

[QUOTE]

fullname (code) ,scheduled arrival,actual arrival,arrived status

[/QUOTE]

here what i want is the livetrainstatus.php defaults print the station code only like GKP,but i want it should be Gorakhpur(GKP).

can anyone suggest the code or logic.

so that it will help me a lot.

Thank u so much for reading this.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 11.2016 — Don't understand your question completely:
[LIST]
  • [*]How far is your code working; what is not working?

  • [*]"here what i want is the livetrainstatus.php defaults print the station code only like GKP,but i want it should be Gorakhpur(GKP)." Is this the only problem?

  • [*]E. g. ".$stop['station'].'<center>'.$stop['scharr']": I do not find such members in your JSON. I do not find any of the members you are referencing in your foreach loop in your JSON.

  • [/LIST]
    Copy linkTweet thisAlerts:
    @livetrainstatusauthorJan 12.2016 — Hi sempervivium,

    Station and scharr all the members I will get the below json response

    {"response_code":200,"error":"","position":"Train has reached Destination and late by 5 minutes.","train_number":"12046","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},{"no":3,"station":"KUN","has_arrived":true,"has_departed":true,"day":0,"distance":142,"scharr":"13:36","schdep":"13:38","actarr":"13:57","actdep":"13:59","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":21},{"no":4,"station":"NDLS","has_arrived":true,"has_departed":false,"day":0,"distance":265,"scharr":"15:25","schdep":"Destination","actarr":"15:30","actdep":"00:00","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":5}]}

    Initially I will pass the train number and doj to get all the details from this json response so I have did the foreach loop and for me its producing proper output.

    here you can see the stations in the above the response.

    Here I am getting station code by using the above json response.I want the station full name by matching the another json response.that response too mentioned in the post.

    if u have any queries post here.

    Thanks for your replies.
    Copy linkTweet thisAlerts:
    @livetrainstatusauthorJan 13.2016 — you can provide any one example for me in the below scenario,

    i want to pass the iterated array value using forloop into the url (http://example.com/arrayvalue/)

    then i will get the some json response from that url.

    i have to decode all the response using again for loop and print all the values which got via the url.
    Copy linkTweet thisAlerts:
    @SempervivumJan 14.2016 — Still having problem to understand you. Can you answer this question:
    "here what i want is the livetrainstatus.php defaults print the station code only like GKP,but i want it should be Gorakhpur(GKP)." Is this the only problem?[/QUOTE]
    ×

    Success!

    Help @livetrainstatus spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...