Skip to content
Advertisement

Having trouble wrapping my head around how to write a particular loop

I am querying a SQL database to return open work order operations.
My query is producing the Work Order number, operation status and due date.
I am trying to figure out how to iterate through the array that is returned and:

  • Gather the sum of operations due within a week
  • Gather the sum of operations due in the second week
  • Continue to do this until I have made it through all of the entries, ongoing for as many weeks as necessary.

My SQL query looks something like:

It will return something like:

Either way, what I am trying to accomplish is, query the database, step through my result and get the sum foreach week.
While NOW+7days <= DUE_DATE; While NOW+14days <= DUE_DATE…

Week One = 15.5 Hours; Week Two = 10.5 Hours; Week Three = 81.75 Hours

EDIT: I apologize for my mess of a question, this is one of the more intense tasks I have tried to accomplish with SQL and PHP.

We are trying to get a better handle on our capacity, and reporting on our capacity.
I am hoping to be able to run a query that pulls all of the ‘280LASERS’ Operations and have some sort of root value (Like todays Date) to compare the DUE_DATE against.

My plan is to sort by DUE_DATE and get the SUM(SETUP_HRS + RUN_HRS) until DUE_DATE is greater than (TODAY() + 7) then, get the SUM(SETUP_HRS + RUN_HRS) until DUE_DATE is greater than (TODAY() + 14) then …

I can’t achieve this with static variables because the number of weeks can go from 6 weeks out, to more than 30 weeks out, simply depending on the DUE_DATE of the furthest out order.

I am so close I can taste it, I would really like to share my code, and the output… but feel I have blown this page up and it is a hot mess. Would it be acceptable for me to delete everything above this and reshare my code as it is, as well as the output I am getting.

Advertisement

Answer

so I’m back, I’ll add a better commented code, here: can’t edit the old answer, since I deleted my account and forgot to cancel 😐

Anyways, you asked how to manipulate the data. It’s a simple array and all the inner arrays are sums from the start of the week to the end. Now, you could store them with different keys, I just used the default assigning because of simplicity.

Edit: A new while loop was added to account for empty weeks. Note d/m/Y is not strtotime recognised format and it will be read as m/d/Y. To convert it refer to this question.

Edit-2: To answer your comment. Ok so the thing about the spans is that I made them so that if the span went from 2020-01-01 to 2020-01-08 and the second one from 2020-01-08 to 2020-01-15 where should the working hours of 2020-01-08 go to week 1 or week 2? When you corrected $entry_time < $future_due[‘end’] to $entry_time <= $future_due[‘end’] this means that the count is added to week 1, while the original solution would have added it to week 2 as the starting date.

You can try and add 8 days and then subtract one if you wanted the containers to span between 2020-01-01 and 2020-01-08 and 2020-01-09 and 2020-01-16 and have both endings be inclusive. Now, I’m not going to write this part since it really depends on how YOU want define your endings.

And your question if you can change the time to something else then the current time? Sure, just change this line.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement