curtm Posted November 28, 2007 Share Posted November 28, 2007 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 More sharing options...
effigy Posted November 28, 2007 Share Posted November 28, 2007 You should be using an array for these. You could then work with slices. Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401531 Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 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. Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401537 Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 I have a PHP script that returns some number values as variables like so... Out of curiosity, can you post that script? Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401546 Share on other sites More sharing options...
Wes1890 Posted November 28, 2007 Share Posted November 28, 2007 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 Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401549 Share on other sites More sharing options...
curtm Posted November 28, 2007 Author Share Posted November 28, 2007 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); ?> Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401564 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2007 Share Posted November 28, 2007 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 />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401574 Share on other sites More sharing options...
curtm Posted November 28, 2007 Author Share Posted November 28, 2007 What if I want to add every 8 days instead of 7 like my original post? (sorry, the user that wants this told me wrong) Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401578 Share on other sites More sharing options...
curtm Posted November 28, 2007 Author Share Posted November 28, 2007 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? Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401579 Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 So does that mean the violation occurs if any X consecutive days totals more than Y hours? Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401585 Share on other sites More sharing options...
curtm Posted November 28, 2007 Author Share Posted November 28, 2007 Correct. 8 consecutive days totalling more than 70 hours = violation Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401594 Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 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. Link to comment https://forums.phpfreaks.com/topic/79324-a-challenging-question-can-you-solve-it/#findComment-401607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.