Trouble with Activity by Company report page

I’m having a bit of difficulty showing results the way I’m hoping to see them. I have a list of activity by each company. The code I have right now lists out the company multiple times if there are multiple activities. If I try to change it to include a group by company, I get only one company, but it selects only the last activity to display.

Here’s the current code:

$query = 'Select * from activity ac, company c where c.company=ac.company order by ac.company';



$result=mysql_query($query); 


if (!$result) {

    $message  = 'Invalid query: ' . mysql_error() . "\
";

    $message .= 'Whole query: ' . $query;

    die($message);

}

while ($row = mysql_fetch_assoc($result)) {

echo '<b><u><font size="5">'.$row['company'].'</font></u></b><br><br>';

echo '<table border="1"><tr><td><b>When:</b>&nbsp;'.$row['timestamp'].'</td>

<td><b>Activity:</b>&nbsp;'.$row['activity'].'</td>

<td><b>Subject:</b>&nbsp;'.$row['subject'].'</td>
</tr></tr></table>'; 

}
?> 

</p>

</div>
</div>
  </body>
</html>

with a current screenie:

quick easy way to do it is by running a sql statement group by company and loop another sql statement based on the company id that’ll give you the results you want.

a fun way to do it is create 2 dimensional array and do for each etc… but its not that serious.

That worked perfectly! Thanks! Now I need to try and understand it :wink:

How about

$previous_company = NULL;
while ($row = mysql_fetch_assoc($result)) {

if ($row['company'] != $previous_company) {
echo '<b><u><font size="5">'.$row['company'].'</font></u></b><br><br>';
}
$previous_company = $row['company'];

echo '<table border="1"><tr><td><b>When:</b>&nbsp;'.$row['timestamp'].'</td>

<td><b>Activity:</b>&nbsp;'.$row['activity'].'</td>

<td><b>Subject:</b>&nbsp;'.$row['subject'].'</td>
</tr></tr></table>'; 

}