Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Large "IF" statement

Posted on 2013-01-25
2
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1600 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
Suggested Courses

610 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