Solved

PHP Large "IF" statement

Posted on 2013-01-25
2
172 Views
Last Modified: 2013-01-25
Good Afternoon Experts, I have a fairly large "IF" statement that I could use some help on.

The idea is I need to loop through our jail database and calculate the time each person has been in jail, my code currently is below:

while($row =& $result->fetchRow(DB_FETCHMODE_ASSOC))
{
	if($row['jm1_active_booking'] != null)
	{
		$count++;
		$sql2 = "select * from jms_02mast where jm2_booking_num = '".$row['jm1_active_booking']."'";
		$result2 = $myConn->query($sql2);
		while($row2 =& $result2->fetchRow(DB_FETCHMODE_ASSOC))
		{
			
			
			
			$diff    = abs(strtotime($currentDate) - strtotime($row2['jm2_book_date']));
			$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
			
			if($days >1 && $days <6)
			{
				$OnetoFiveDays++;
			}
			if($days >5 && $days < 31)
			{
				$FivetoThirtyDays++;				
			}
			if($days > 30 && $days < 61)
			{
				$ThirtyOnetoSixtyDays++;
			}
			if($days > 60 && $days < 91)
			{
				$SixtyOnetoNintyDays++;
			}
			if($days > 90 && $days < 121)
			{
				$NintyOnetoOneTwentyDays++;
			}
			if($days >120 && $days < 181)
			{
				$OneTwentyOnetoOneEightyDays++;
			}
			if($days >180 && $days <366)
			{
				$OneEightyOnetoThreeSixtyFiveDays++;
			}
			if($days > 365)
			{
				$OverThreeSixtyFiveDays++;
			}
			if($days < 1)
			{
				$lessthanday++;
			}
	}

Open in new window


Currently it works fine when I print out the different variables.  My question is:

1) if $days does not match any of those conditions (Lets say I screw something up and want to be able to double check) I would like it to print the values that do not match anything

2) Is this a bad way to do this?  I know it WORKS, but it be better using an array, or something else I'm not thinking of? Or does it not really matter?

Thanks,
0
Comment
Question by:hej613
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 38819882
you could use the elseif statement:
http://php.net/manual/en/control-structures.elseif.php
and simplify like this:
                  ....
                       if($days < 1)
			{
				$lessthanday++;
			}
			elseif ( $days <6)
			{
				$OnetoFiveDays++;
			}
			elseif($days < 31)
			{
				$FivetoThirtyDays++;				
			}
  ... etc ...
			
	}

Open in new window

you don't need to retest each time the $days > 1, because you excluded that already
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 100 total points
ID: 38820011
greetings  hej613, , , I looked at your code, and you really should Have the very first thing you do for the $days test is to see if it is in a workable range,maybe as
// $crap is an array you create before the while loop to have ERROR for Days
if(($days < 0) || ($days > 365)) {
    $crap['user'] = $row2['userID'];// some way to find the user with the error
    $crap['days'] = $days;// see what the ERROR is about
    // add other to $crap to help you correct the error
    continue; // you need to learn about using continue; and break; in LOOPS, very useful to know
    }

page about using continue;  -
http://php.net/manual/en/control-structures.continue.php

if you need to get ranges for a variable you will likely use the IF tests that you do now, but as has been suggested use the PHP  elseif( )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now