Jump to content

A challenging question - can you solve it?


curtm

Recommended Posts

I have a PHP script that returns some number values as variables like so...

 

$day01 = 5

$day02 = 8

...

$day31 = 6

 

I want to find out a way to easily see if any 7 in a row of the day variables totals to more than 70.

 

So if $day01 + $day02 + ... + day07 = 69, do nothing,

 

but if $day05 + day06 + ... + $day11 = 71, echo "Over 70 violation!".

 

Is there a way to do this easily or am I going to have to make a WHOLE BUNCH of If statements?

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/
Share on other sites

Instead of 31 variations of the variable $day, next time use a single array named $day with 31 keys (0 through 31).  It does simplify this sort of thing a little bit.

 

Anyways, here you go:

<?php
  for($i = 1; $i <= 25; $i++){
    $total = 0;
    for($j = 0; $j < 7; $j ++){
      $n = sprintf("%02d", $i + $j);
      $total += ${'day' . $n};
      if($total > 70){
        break;
      }
    }
    if($total > 70){
      // VIOLATION
    }
  }
?>

 

That's the inefficient, brute force method.

 

(edit) Outer loop should be less than equal to 25, not 24 as in my first post.

do this:

 

// do this for each day
$day = array(
"1"  =>  5,
"2"  =>  8,
"3"  =>  12
);

// now, you can echo each day's value like this $day['1'].. which would output 5

//so to add them all together do this
$total = 0; // start at 0

for ($i=0; $i<count($day); $i++)
{
  $total += $day[$i]; // add each day
}

echo $total; // this will be the final

Here is the code, and it works now!

 

<?php 

//connect to database 
$connectionstring = odbc_connect("as400", "user", "password"); 

//SQL quyery  
$Query = "select DLDY01,DLDY02,DLDY03,DLDY04,DLDY05,DLDY06,DLDY07,DLDY08,DLDY09,DLDY10,DLDY11,DLDY12,DLDY13,DLDY14,DLDY15,DLDY16,DLDY17,DLDY18,DLDY19,DLDY20,DLDY21,DLDY22,DLDY23,DLDY24,DLDY25,DLDY26,DLDY27,DLDY28,DLDY29,DLDY30,DLDY31, DRCODE from IESFILE.DRVRHOUR, IESFILE.DRIVERS where DLDRVR = DRCODE and DRTDAT = 0 and MONTH(CURRENT_DATE)=DLMNTH and DLYEAR = YEAR(CURRENT_DATE)
order by DRCODE"; 

//execute query 
$queryexe = odbc_do($connectionstring, $Query);


//query database
while(odbc_fetch_row($queryexe)) 
    {

    //collect results 
    $drcode = odbc_result($queryexe, 32); 
    $day01 = odbc_result($queryexe, 1); 
    $day02 = odbc_result($queryexe, 2); 
    $day03 = odbc_result($queryexe, 3); 
    $day04 = odbc_result($queryexe, 4); 
    $day05 = odbc_result($queryexe, 5); 
    $day06 = odbc_result($queryexe, 6); 
    $day07 = odbc_result($queryexe, 7); 
    $day08 = odbc_result($queryexe, ; 
    $day09 = odbc_result($queryexe, 9); 
    $day10 = odbc_result($queryexe, 10); 
    $day11 = odbc_result($queryexe, 11); 
    $day12 = odbc_result($queryexe, 12); 
    $day13 = odbc_result($queryexe, 13); 
    $day14 = odbc_result($queryexe, 14); 
    $day15 = odbc_result($queryexe, 15); 
    $day16 = odbc_result($queryexe, 16); 
    $day17 = odbc_result($queryexe, 17); 
    $day18 = odbc_result($queryexe, 18); 
    $day19 = odbc_result($queryexe, 19); 
    $day20 = odbc_result($queryexe, 20); 
    $day21 = odbc_result($queryexe, 21); 
    $day22 = odbc_result($queryexe, 22); 
    $day23 = odbc_result($queryexe, 23); 
    $day24 = odbc_result($queryexe, 24); 
    $day25 = odbc_result($queryexe, 25); 
    $day26 = odbc_result($queryexe, 26); 
    $day27 = odbc_result($queryexe, 27); 
    $day28 = odbc_result($queryexe, 28); 
    $day29 = odbc_result($queryexe, 29); 
    $day30 = odbc_result($queryexe, 30); 
    $day31 = odbc_result($queryexe, 31); 
    
         
     for($i = 1; $i <= 25; $i++){
    $total = 0;
    for($j = 0; $j < 7; $j ++){
      $n = sprintf("%02d", $i + $j);
      $total += ${'day' . $n};
      if($total > 70){
      	break;
      }
    }
    if($total > 70){
      // VIOLATION
      
      echo $drcode;
      echo "Violation!!!!";
      echo "<br>";
    }
  }
    
    
     
   



    } 
     
    //disconnect from database 
    odbc_close($connectionstring); 

    ?> 

If those were put into an array, here is the minimum code that would work for any length month -

 

<?php
// $day is an array with index 1 to last day of the month (28, 29, 30, or 31)
$end = count($day) - 6;
$max = 70;
for($i = 1; $i <= $end; $i++)
{
if(($sum = array_sum(array_slice($day,$i - 1 ,7))) > $max)
{
	Echo "Limit exceeded, I: $i, sum: $sum<br />";
	break;
} else {
	echo "Limit OK, I: $i, sum: $sum<br />";
}
}
?>

And now for the twist....

 

The number values are hours and minutes, so 11 hours and 15 minutes on day 4 shows like this...

 

$day04 = 11.15

 

How can I make the computer think that .60 = 1  for adding purposes?

 

Your best bet would probably be to store each of the $dayXX variables as minutes, rather than HH.MM.

 

To do that:

list($h, $m) = explode('.', odbc_result($queryexe, 1));
$day01 = $h * 60 + $m;

 

Then instead of comparing to 70 for the violation, compare to 70 * 60 (the amount of minutes in 70 hours).

 

As for the 7 consecutive days changing to 8, just change the bounds on the loops.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.