Solved

PHP Large "IF" statement

Posted on 2013-01-25
2
174 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 143

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 34

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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