rbarnett Posted June 17, 2008 Share Posted June 17, 2008 I have a query that I display to the screen using a foreach. The query has driver id, date, miles, odometer reading and when I display the results I want to display each miles and odometer reading by date instead of showing multiple driver ids with the the miles and odometer reading for each. Here is the related code: $sql = "select distinct driver_id, username, client_employee_id, last_name, first_name, name, miles, odometer, date from drivers join driver_groups dg using(driver_group_id) join users u using(contact_id) join contacts c using(contact_id) join monthly_mileages using (driver_id) where company_id=23342 and date BETWEEN '2008-2-01' AND '2008-5-01' ORDER BY last_name"; $aOutput = $oDb->getArrayOfArrays("$sql"); // returns result of query echo '<table><tr> <td>Driver ID</td><td>Miles</td><td>Odometer</td> </tr>'; foreach ($aOutput as $value) { echo '<tr><td>'.$value['driver_id'].'</td><td>'.$value['date'].'</td><td>'.$value['miles'].'</td><td>'.$value['odometer'].'</td></tr>'; } echo '</table>'; The html output looks like: Driver ID Date Miles Odometer 24013 2008-05-01 1241 35712 24013 2008-04-01 1819 34051 24021 2008-04-01 29 84216 24021 2008-05-01 179 85419 24472 2008-05-01 1335 69071 24472 2008-04-01 1942 67586 I instead want the output to look like Driver ID April08 April08 Miles April08 Odometer May08 Miles May08 Odometer 24013 2008-04-01 1241 35712 1819 34051 24021 2008-04-01 29 84216 179 84216 24472 2008-04-01 1335 69071 1942 67586 I really don't want to change the query in order to do this. Does anyone have any suggestions? Thanks, Quote Link to comment Share on other sites More sharing options...
craygo Posted June 17, 2008 Share Posted June 17, 2008 what you have to do is tell php to change the driver id when it is different <?php $lastdriver = ''; foreach ($aOutput as $value) { if($value['driver_id'] != $lastdriver){ echo "</tr><tr><td>".$value['driver_id']."</td>\n"; } echo '<td>'.$value['date'].'</td><td>'.$value['miles'].'</td><td>'.$value['odometer'].'</td>'; $lastdriver = $value['driver_id']; } ?> Ray Quote Link to comment Share on other sites More sharing options...
rbarnett Posted June 17, 2008 Author Share Posted June 17, 2008 Thanks! This works perfectly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.