Jump to content

[SOLVED] Array Display newbie question


rbarnett

Recommended Posts

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,

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.