You only need a good MySQL query.
See here .
You can do additions, substrings and things like date BETWEEN x AND Y , you can do SELECT SUM() with GROUP BY and so on.
What Hakan means (I think) is that you are doing it wrong: you must first complete a query that will do almost all the work for you. No need to develop such a complex thing.
And three more tips:
- try to avoid keywords in php like
$expense->Date . This causes syntax highlighting problems (at best, at worst, Php doesn't understand your code). - add more comments to your code to explain what you are trying to do.
- try to avoid keywords in PHP AND queries. You have a column named "
Date " and a column named " Type ". It is not safe.
This is just the beginning of what your SQL might look like, and should almost cover 95% of your code. Note: this sentence: let the entire database server do the work for you, this is done for this:
SELECT ps.Due,ps.Date, wt.Amount,wt.Date, ex.Amount,ex.Date LEFT JOIN patient_sessions ps ON xxx WHERE ps.Type='Session' AND ps.Date BETWEEN DATE_ADD(NOW(), INTERVAL '-28' DAY) AND DATE_ADD(NOW(), INTERVAL 1 DAY) LEFT JOIN work_times wt ON xxx LEFT JOIN expenses ex ON xxx WHERE ex.Client='Psychotherapy'
source share