PHP:使用IF ELSE语句输出错误
Here's my database sample:
**Netname** **BldgName** **Totalduration**
LRT - 2 Araneta Center - Cubao 09:30:00
LRT - 2 Legarda 09:45:00
LRT - 2 Pureza 09:50:00
LRT - 2 Santolan 10:00:00
LRT - 2 Recto Station 09:55:00
Then I have a combobox for Duration...
Duration
5 s
10 s
15 s
30 s
60 s
Here's my code:
if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1") && $row[fldTotalDuration] == "10:00:00")
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/full.png' title='Already full'><br>";
}
else if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1")&& $row['fldTotalDuration'] >= "09:55:00" && $row['fldTotalDuration'] <= "10:00:00" && $get_duration >= "6 s" && $get_duration <= "60 s" )
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/near_full.png' title='Only 5 s left'><br>";
}
else if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1")&& $row['fldTotalDuration'] >= "09:50:00" && $row['fldTotalDuration'] <= "09:54:00" && $get_duration >= "11 s"&& $get_duration <="60 s" )
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/near_full.png' title='Only 10 s left'><br>";
}
else if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1")&& $row['fldTotalDuration'] >= "09:45:00" && $row['fldTotalDuration'] <= "09:49:00" && $get_duration >= "16 s" && $get_duration <= "60 s")
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/warning.png' title='Only 15 s left'><br>";
}
else if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1")&& $row['fldTotalDuration'] >= "09:30:00" && $row['fldTotalDuration'] <= "09:44:00" && $get_duration >= "31 s" && $get_duration <= "60 s")
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/warning.png' title='Only 30 s left'><br>";
}
else if(($get_ID == "LRT - 2" || $get_ID == "LRT - 1")&& $row['fldTotalDuration'] >= "09:00:00" && $row['fldTotalDuration'] <= "09:29:00" && $get_duration > "60 s")
{
echo " ";
echo $row['fldBldgName'];
echo " <img src='image/warning.png' title='Only 60 s left'><br>";
}
Sample Scenario:
I choose "5 s" for duration...
Here's the output of my program, :
This one is wrong....
The output should be..
This one is the correct output:
because I will just add only "5 seconds" in the total duration....the rest duration is working but instead of this one [5 sec]
Thanks for the help..
As requested by the OP: the problem lies with the way $get_duration
's value is evaluted:
$get_duration <= "60 s"
for example... comparing strings is always going to be troublesome:
var_dump('9s' > '4s');//true
//but:
var_dump('10s' > '5s');//false!
var_dump('9s' > '50s');//true!
This is because strings are compared differently:
'9s' > '50s'
Probably best understood if you look at C's way of dealing with strings: '9' => 57 (ASCII), whereas '5' is actually 53, and since 57 > 53, the comparison of the two strings equates to true. especially if you consider 0
is actually 48, but s
is actually 115.
Of course, PHP will spot cases where you're actually comparing '12' > '9'
, and compare the integer vals instead (12 > 9
). But, to be safe, I personally prefer casting my variables to the right type. That way, I'm sure of what type given variable contains
When comparing numbers, compare the integer/float values, not string representations of them.
Basically, replace all those "60 s"
string constants with 60
. As the OP mentioned: this solves the problem/.
I am not going to try and figure out what exactly you are doing here. Just going to recommend using the Switch command, rather than a load of nested if/elseif statements.
If I see that you hard-code the LRT - 2 in there, I assume you could make things better also by formulating your database query differently.