Hi everyone!
I am trying to optimize a script where I perform a query of a [font=courier]jobs[/font] table. Jobs are recorded by [font=courier]department[/font] and by [font=courier]contractor[/font]. Currently, what I do is I query the jobs table, determine the unique department numbers and then perform another query for each department number and get the job data.
This is the skeleton of my script:
[php]
$getdepts = $db->get_departments($startdate, $enddate);
foreach ($getdepts as $dept) { // cycle through all departments with recorded time entries
// get job data for invoice
$getjobs = $db->get_job_data($invoice_department, $startdate, $enddate);
// process queried rows
foreach ($getjobs as $job_row) { // build a row for each job serviced
} // foreach getjobs
# display the department invoice
} // foreach getdepts
[/php]
As you can see, for every [font=courier]department[/font], my script queries the database for each job row. Even though this works, I am sure there is a more efficient way to do it, by querying all the jobs between the start and end dates into an array and then parsing through the array.
So far, this is what I have:
[php]
$jobs = $db->get_jobs($startdate, $enddate);
$depts = array();
foreach ($jobs as $job) {
$depts[] = $job[‘job_dept’];
}
$uniqueDepts = array_unique($depts);
[/php]
So now I have an array with all the unique [font=courier]department[/font] numbers, and this is where I am stuck.
How can I parse the entire array ([font=courier]$jobs[/font]) and echo all the rows grouped by [font=courier]department[/font] number?
Thanks!