9 Replies - 773 Views - Last Post: 12 May 2014 - 12:41 PM Rate Topic: -----

#1 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6006
  • View blog
  • Posts: 10,435
  • Joined: 28-September 07

If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:12 AM

Aloha, comrades! Here's my problem. I have a form where a user inputs a daily start time and end time in the following manner:

Attached Image
"H" is for hour and includes the numbers 1-12.
"M" is for minutes and includes the options 00, 15, 30, and 45.
"T" is for time of day and includes AM and PM.

Upon form submission, each day's hours are checked for a value !='' ("H", "M", and "T" all have a default value of ''). If actual values are found, calculations are performed. If not, the day is set to "None" and the day's total hours are set to "0". Like so (using Saturday because it's the specific piece of code I'm playing with at the moment):
//SATURDAYS
if(($_POST['satsh']!='')&&($_POST['satsm']!='')&&($_POST['satst']!='')&&($_POST['sateh']!='')&&($_POST['satem']!='')&&($_POST['satet']!='')) {
     $sat=$_POST['satsh'].":".$_POST['satsm']." ".$_POST['satst']." - ".$_POST['sateh'].":".$_POST['satem']." ".$_POST['satet'];
     //Calculate session hours.
} else {
     //No Saturday session.
     $sat="None";
     $sathrs=0;
}



Because part of my job on this project is to calculate the number of hours in a particular day's session - I've done the following under the "//Calculate session hours." comment:
//Convert start hours to 24-hour format.
if ($_POST['satst']=='AM') {
     $start=$_POST['satsh'];
} else {
     $start=$_POST['satsh']+12;
}
//Convert end hours to 24-hour format.
if ($_POST['satet']=='AM') {
     $end=$_POST['sateh'];
} else {
     $end=$_POST['sateh']+12;
}
//Calculate session hours.
     $hrs=$end-$start;
//Convert start minutes to decimal value.
if ($_POST['satsm']=='00') {
     $sm=0.00;
} else if ($_POST['satsm']=='15') {
     $sm=0.25;
} else if ($_POST['satsm']=='30') {
     $sm=0.50;
} else {
     $sm=0.75;
}
//Convert end minutes to decimal value.
if ($_POST['satem']=='00') {
     $em=0.00;
} else if ($_POST['satem']=='15') {
     $em=0.25;
} else if ($_POST['satem']=='30') {
     $em=0.50;
} else {
     $em=0.75;
}
//Calculate session minutes.
     $mins=$em-$sm;
//Calculate total hours for day.
     $total=$hrs+$mins;
//Verify total hours is positive number.
if($total>0) {
     $sathrs=$total;
} else {
     $sathrs="Error";
}



Later on, I'm echoing this information, like so:
if ($sathrs>0) {
     echo "Sat: ".$sat." (".$sathrs." hours)<br />";
} else if ($sathrs=="Error") {
     echo "Sat: ".$sat." (ERROR)<br />";
} else {
     // No Saturday hours.
}


Essentially, if there are hours on Saturday, I want it to display the start and end times and total hours for the day. If there are hours with mismatched start and end times (ie, start time is PM, end time is AM), I want it to display the start and end times and list ERROR for the total hours for the day. If there are no hours for the day, I don't want it to display anything at all.

I also have the following line of code so I can see what actual values I'm dealing with:
<?php echo "Control: ".$sat." ".$sathrs; ?>



Test data set 1:
Attached Image

Output 1:

Quote

Sat: 2:00 AM - 2:15 AM (0.25 hours)
Control: 2:00 AM - 2:15 AM 0.25


Test data set 2:
Attached Image

Output 2:

Quote

Sat: None (ERROR)
Control: None 0


Test data set 3:
Attached Image

Output 3:

Quote

Sat: 2:00 PM - 2:15 AM (ERROR)
Control: 2:00 PM - 2:15 AM Error


The problem lies in data set 2. There should be no echo outside of the Control line because the $sathrs variable is equal to 0. It should most assuredly not be displaying (ERROR) as it is not set to "Error" and therefore should not be entering the else if part of the statement, right?

Where am I messing up my logic here?

Any help you lovely people can provide will be immensely appreciated. Thank you!!

Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: If-Else Not Yielding Desired Result

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:25 AM

well lets knock out this real quick and make sure you just typoed...

if (sathrs>0) { //<-- No dollar sign on sathrs
  echo "Sat: ".$sat." (".$sathrs." hours)<br />";




:)
Was This Post Helpful? 1
  • +
  • -

#3 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6006
  • View blog
  • Posts: 10,435
  • Joined: 28-September 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:31 AM

Sadly, yes, just typos. :cry2:
Fixed.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 1,292
  • Joined: 15-January 14

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:31 AM

First, there's a much easier way to figure that stuff out. If you use something like mktime or strtotime to turn your times into Unix timestamps, then you can just get the difference and divide to figure out how many hours there are. You could also create DateTime objects and get the difference between them that way.

You should be able to trace your code back though. It is printing "Error" because $sathrs is set to "Error", and that got set to "Error" because $total is not greater than 0. So you're always printing "Error" if the total number of hours is not greater than 0. I would probably just use $total and check that on the bottom. If it's 0, don't show an error. If you want to figure out if the start time is later than the end time then you can do that with the timestamps.
Was This Post Helpful? 2
  • +
  • -

#5 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6006
  • View blog
  • Posts: 10,435
  • Joined: 28-September 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:37 AM

View PostArtificialSoldier, on 12 May 2014 - 02:31 PM, said:

First, there's a much easier way to figure that stuff out. If you use something like mktime or strtotime to turn your times into Unix timestamps, then you can just get the difference and divide to figure out how many hours there are. You could also create DateTime objects and get the difference between them that way.


An example of this would be fantastic. Anything I can do to make this process simpler is great. I'm trying to step through the logic involved in this project as literally and linearly as possible. If your idea will work within the bounds of my front-end requirements, that would be fantastic.

View PostArtificialSoldier, on 12 May 2014 - 02:31 PM, said:

You should be able to trace your code back though. It is printing "Error" because $sathrs is set to "Error", and that got set to "Error" because $total is not greater than 0. So you're always printing "Error" if the total number of hours is not greater than 0. I would probably just use $total and check that on the bottom. If it's 0, don't show an error. If you want to figure out if the start time is later than the end time then you can do that with the timestamps.


It's only setting $sathrs to "Error" if the time is set. If you look in the spoiler, I put all the code together. In data set 2 - as is shown in the control line - $sathrs is actually set to 0... yet it is still entering the if $sathrs=='Error' part of the code.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 1,292
  • Joined: 15-January 14

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:44 AM

Here's the reference for mktime:

http://www.php.net/m...tion.mktime.php

I would use default values for today's date, and then fill in your own hours and minutes. e.g.:

$start = mktime($_POST['satsh'] + ($_POST['satst'] == 'PM' ? 12 : 0), $_POST['satsm'], 0);
$end = mktime($_POST['sateh'] + ($_POST['satet'] == 'PM' ? 12 : 0), $_POST['satem'], 0);

if ($start > $end) {
  // start time is after end time
}

$elapsed_hours = ($end - $start) / 3600;

Was This Post Helpful? 3
  • +
  • -

#7 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6006
  • View blog
  • Posts: 10,435
  • Joined: 28-September 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:49 AM

That is awesome! Thank you! That will save me a boatload of code!
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:52 AM

B9...

What do you think the result of this is?

if (0 == "Error") {
   echo "True";
}
else {
   echo "False";
}



Keep in mind of conversions. ;)


Answer: "Error" is going to be type juggled to be an integer equal to zero. Thus the answer is "True"

:)

This post has been edited by Martyr2: 12 May 2014 - 11:53 AM

Was This Post Helpful? 2
  • +
  • -

#9 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 11:56 AM

edit: Martyr2 beat me to it :P
By the way, Martyr2, I just bought your programming ideas book. Great stuff so far! This will definitely keep me busy for quite some time :).
For what it's worth, it seems that $sathrs is indeed 0 when it reaches line 57 in your code in spoilers. I did some testing and found out that 0 == "string" evaluates to TRUE. A quick search found this StackOverflow post which says:

Quote

Your first example (a 'loose' comparison since it does not also check the data types of the two operands) implicitly converts the string on the left to an integer. Since it does not start with a number, the string is converted to the integer 0, which is equal to the integer 0.

This post has been edited by Sho Ke: 12 May 2014 - 11:58 AM

Was This Post Helpful? 2
  • +
  • -

#10 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6006
  • View blog
  • Posts: 10,435
  • Joined: 28-September 07

Re: If-Else Not Yielding Desired Result

Posted 12 May 2014 - 12:41 PM

Ooooh.... I get it.

The new code:
//SATURDAYS
if(($_POST['satsh']!='')&&($_POST['satsm']!='')&&($_POST['satst']!='')&&($_POST['sateh']!='')&&($_POST['satem']!='')&&($_POST['satet']!='')) {
     $sat=$_POST['satsh'].":".$_POST['satsm']." ".$_POST['satst']." - ".$_POST['sateh'].":".$_POST['satem']." ".$_POST['satet'];
     //Calculate session hours.
          $start=mktime($_POST['satsh']+($_POST['satst']=='PM'?12:0),$_POST['satsm'],0);
          $end=mktime($_POST['sateh']+($_POST['satet']=='PM'?12:0),$_POST['satem'],0);
          if($start>$end) {
               $sathrs="Error";
          } else {
	       $sathrs=($end-$start)/3600;
	  }
} else {
     //No Saturday session.
     $sat="None";
     $sathrs="None";
}

if ($sathrs>0) {
     echo "Sat: ".$sat." (".$sathrs." hours)<br />";
} else if ($sathrs=="Error") {
     echo "Sat: ".$sat." (ERROR)<br />";
} else {
     // No Saturday hours.
}
echo "Control: ".$sat." ".$sathrs;



Works like a charm now! And it's so much prettier!

Thank you all sooooo much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1