How to use the nested ternary operator in php?

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 nested ternary operator in php?

Post by sathya »

Hi ,

I have three different status for the train as below,

1.Train Not Started
2.Train Departed
3.Train Not Reached.


In the below php code i am getting only the two outputs below using ternary operator.

Train Departed and Train Not Reached.

Code: Select all

<!DOCTYPE html>  
<HTML>  
  <HEAD>  
    <TITLE>Train Status Time Table</TITLE>  
    <style>  
      table { width:60%; }  
      table, th, td {  
        border: 1px solid black;  
        border-collapse: collapse;  
      }  
      th, td {  
        padding: 5px;  
        text-align: left;  
      }  
      table#t01 tr:nth-child(even) {  
        background-color: white;  
        width:60%;  
      }  
      table#t01 tr:nth-child(odd) { background-color:white; }  
      table#t01 th    { 
        background-color: blue;  
        color: white;  
      }  
      table#t02 th    { 
        background-color: blue;  
        color: white;  
      }  
      body { background-color: lightblue; }  
      ul {  
        list-style-type: none;  
        margin: 0;  
        padding: 0;  
        overflow: hidden;  
        background-color: #333;  
        width: auto;  
      }  
      li { float: left; }  
      li a {  
        display: block; 
        color: white;  
        text-align: center;  
        padding: 14px 16px;  
        text-decoration: none;  
      }  
      li a:hover:not(.active) { background-color: #111; }  
      .active { background-color: #4CAF50; }  
    </style>  
  </HEAD>  
  <BODY>  
    <CENTER><H2><B>Live Train Running Status</B></H2></CENTER>  
    <UL>  
      <LI><A class="active" href="#home">Home</A></LI>  
      <LI><A href="#news">Live Train Status</A></LI>  
      <UL style="float:right;list-style-type:none;">  
        <LI><A href="#about">Blog</A></LI>  
        <LI><A href="#login">Contact</A></LI>  
      </UL>  
    </UL>  
    <BR />  
    <CENTER>  
<?php  
  $json = '';  
  if(isset($_GET['trainnumber']) && (isset($_GET['doj']))) {   
    $url = 'http://api.railwayapi.com/live/train/'.$_GET['trainnumber'].'/doj/'.$_GET['doj'].'/apikey/*/';  
    $url_trainname = 'http://api.railwayapi.com/name_number/train/'.$_GET['trainnumber'].'/apikey/*/';  
    $json = json_decode(file_get_contents($url), true);  
    $json_trainname = json_decode(file_get_contents($url_trainname), true);  
    $trainname=$json_trainname ['train']['name'];  
    $trainnumber = $json['train_number'];  
    $doj = $json['route'][0]['scharr_date'];  
    $position = $json['position']; 
?>  
      <TABLE id='t01'>  
        <TR><TH>Train Number</TH><TH>Date on Journey</TH></TR>  
        <TR><TD><?= $trainnumber; ?></TD><TD><?= $doj; ?></TD></TR> 
        <TR><TH>Position</TH></TR>  
        <TR><TD><?= $position; ?></TD></TR> 
        <TR><TH>Train Name</TH></TR>  
        <TR><TD><?= $trainname; ?></TD></TR> 
      </TABLE> 
    <TABLE id='t01'>   
        <TR><TH>Station</TH><TH>Scheduled</TH><TH>Actual</TH><TH>Status/Delay</TH></TR>  
<?php 
    foreach($json['route'] as $stop) {  
      $url_trainnameconvert = 'http://api.railwayapi.com/code_to_name/code/'.$stop['station'].'/apikey/neoid4725/'; //passing the station element from Json Response 1 to Json Response 2  
      $json_trainnameconvert = json_decode(file_get_contents($url_trainnameconvert), true);  
$maxname=$json_trainnameconvert['stations'][0]['fullname'];

 ($stop['has_departed'] === true) ? $stop['has_departed'] = 'Departed' : $stop['has_departed'] = 'Still not reached here'; // Train Departed and Train Not Reached status displayed here.here i want one more status as train not started with delay time.
 
 
        echo '        <TD>'.$maxname.'</TD><TD>'.$stop['scharr'].'</TD><TD>'.$stop['actarr'].'</TD><TD>'.$stop['has_departed']."</TD></TR>\n"; 
    }   
  } else {  
    echo 'Something went wrong, please notify to admin '; 
  }  
?>  
      </TABLE> 
    </CENTER>  
  </BODY>  
</HTML>

I want to check the status one by one and produce the result .

1. $position has the text as Train is currently at Source and late by 0 minutes then it should print the Not Started as output.

else

2.$stop['has_departed'] === true and $stop['latemin'] === 0 mins late then it should print the status as departed

else
3.$stop['has_departed'] === true and !$stop['latemin'] === 0 mins late then it should print the status as departed/latemin

else

4.if the above conditions not okay it should print the value as Not reached.

Kindly help me to find the solution.
Last edited by requinix on Wed Feb 03, 2016 11:35 pm, edited 1 time in total.
Reason: removing api key by request
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use the nested ternary operator in php?

Post by Celauran »

How to use the nested ternary operator in php?
Don't. Seriously. It quickly becomes an indecipherable mess.
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: How to use the nested ternary operator in php?

Post by sathya »

oh okay.sorry if we cant use the ternary operator help me to get the solution for if/elseif/else..
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use the nested ternary operator in php?

Post by Celauran »

Sure. What have you tried so far?
sathya
Forum Commoner
Posts: 72
Joined: Sat Dec 12, 2015 7:26 am
Contact:

Re: How to use the nested ternary operator in php?

Post by sathya »

i have tried the below one .

Code: Select all

if ($position="Train is currently at Source and late by 0 minutes." && $stop['has_departed'] == false)
 {
 echo "Train Not Started";
 }
 else if($stop['has_departed'] == false && $stop['latemin'] == 0 mins late)
 {
 
  echo "Departed/No Delay";
  }
  else if ($stop['has_departed'] == false && !$stop['latemin'] == 0 mins late)
  {
  echo "Departed"$latemin;
  }
  
  else
  {
  echo "Not Reached";
  }
if any one giving the solution in if/else to get the status thats too very fine.
Last edited by Celauran on Thu Jan 21, 2016 11:23 am, edited 1 time in total.
Reason: Please wrap your code in syntax tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to use the nested ternary operator in php?

Post by Christopher »

First, careful using = instead of == in conditions!

Code: Select all

if ($position == "Train is currently at Source and late by 0 minutes." && $stop['has_departed'] == false)
Second, all of these conditions evaluate whether $stop['has_departed']. How about:

Code: Select all

if($stop['has_departed'] {
  echo "Has departed. ";
} else {
  if ($position == "Train is currently at Source and late by 0 minutes.") {
    echo "Train Not Started";
  } elseif ($stop['latemin'] == 0 mins late) {
    echo "Departed/No Delay";
  } elseif (!$stop['latemin'] == 0 mins late) {
    echo "Departed"$latemin;
  } else {
    echo "Not Reached";
  }
}
You probably need to clean up the else conditions of "Has departed" and "Not reached".
if any one giving the solution in if/else to get the status thats too very fine.[/quote]
(#10850)
Post Reply