I would appreciate some help with php arrays. I need to create a graph, but I can’t seem to get the values from database the way I would like them to.
So basically what I need are hours per month for certain job.
Database looks something like this:
Date | Job | Hours
2012-01-01 | 104 | 8.5
Every job has a different number, but I hope this helps a little.
I need to get the sum of hours per month spent on one job.
So far, I have made an array for months and looped through them:
$month_date_array = array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
foreach ($month_date_array as $month) {
$sql = "SELECT * FROM database WHERE date LIKE '%-$month-%'";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$job[] = $row['job'];
}
}
Okei, I made some changes and I am going to update my question:
The year does matter, it can’t group 2011 and 2012 together. The year comes from a dropdown menu – the user chooses what year.
Next I added an array of jobs, because I just figured out that I can’t take all jobs, so the job array looks something like this:
$job = array('22', '6', '12', '55');
Basically I would like to know how to get the sum of hours what somebody spent on doing one job per month. And I don’t want to do it with SQL, I need arrays of some sort, because later I have to put together a graph.
If you want to learn how to manipulate and do this with arrays:
But know that this is actually going to group all the years together. In other words November 2011, 2012 and 2013 will be added together if those dates exist.
You could also do this grouping and adding with SQL only.
http://sqlfiddle.com/#!2/416d9/5/0
or http://sqlfiddle.com/#!2/416d9/6/0