Solved

PHP Large "IF" statement

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now