title: PHP Group items in a report table
body: <p>Designing a report that is displayed (PHP outputting HTML) in tables.  Data looks like this (coloring not mine):</p>
  
  <pre><code>Date         PID  Client Name  Activity         Hours
  2012-10-15   52   Company Y    Report writing   3.5
  2012-10-15   16   Company D    Meeting          1.3
  2012-10-15   21   Company F    Telecon          0.3
  2012-10-15   52   Company Y    Telecon          0.8
  2012-10-16   16   Company D    Client visit     1.7
  2012-10-16   52   Company Y    Preparation      1.8
  2012-10-16   16   Company D    Edit MMS 12      7.1
  </code></pre>
  
  <p>I've written everything to get the data from MySQL, extract it line-by-line (<code>mysql_fetch_assoc</code>), construct an HTML table that contains the values, and output. Basic stuff - did it through jQuery calling Ajax, looks great.</p>
  
  <p>I've now been asked to group the items by CID/CompanyName (same thing) and output (I figure as separate tables) with a summary total of hours-per-company below each companytable.</p>
  
  <p>I'd like to use only one mysql_query, so that means doing the groups/calcs in PHP.</p>
  
  <p>I've just worked through a 3-day weekend finishing a db restructure and writing the initial report and I'm having difficulty thinking this through. Would a kind soul help me with the approach? I've started several different algorithms and they spaghetti out of control. I know there's an elegant, simple approach but at the moment I cannot conceive of it.</p>