Friday, 16 August 2013

Formatting MySQL Results in PHP by Groups based on conditions

Formatting MySQL Results in PHP by Groups based on conditions

I have data from two different tables that I join together and run a query
against.
What I am trying to achieve is that my results will group based on the
order number, and possibly one other condition - if it works out.
I am trying to generate price quotes based on damaged items that we
receive back in stock.
One quote can hold multiple work record ids so long as that the order
numbers match up and those order numbers were all entered on the same day.
The one caveat being if one of those work records happens to have an item
that we have to send out to the manufacturer to get an estimate on (which
is a flag in the main database table that I'm bringing in.
For now though, I would like to take the data I'm getting back and display
it on the screen as a preview of what the quotes will contain prior to the
system sending them out.
The end result that I am looking for can be found in this image I've
mocked up:

Here is my query:
$sql = mysql_query("SELECT workRecord_details.orderNumber,
workRecord_main.mfrEstimate, workRecord_main.id,
workRecord_main.createdDate, workRecord_main.nameid,
workRecord_details.dateEntered
FROM workRecord_main
INNER JOIN workRecord_details ON
workRecord_main.id =
workRecord_details.workRecordId
WHERE workRecord_main.billable = '1' AND
workRecord_main.STATUS = '2' AND
workRecord_details.dateEntered LIKE '2013-08-14%'
ORDER BY workRecord_details.orderNumber,
workRecord_main.mfrEstimate, workRecord_main.id"
) or die("Can't execute: " . mysql_error());
if(mysql_num_rows($getWR) > 0){
$i = 0;
while($wrData = mysql_fetch_array($getWR))
{
$color_A = 'class="alt2"';
$color_B = 'class="alt1"';
$row_color = ($i % 2) ? $color_A : $color_B;
echo '<table width = "100%" cellspacing = "0" cellpadding = "0"
border = "0">
<tr>
<td class = "colheader">Work Record #</td>
<td class = "colheader">Company</td>
<td class = "colheader">Order #</td>
<td class = "colheader">Description</td>
</tr>';
echo '<tr>
<td width= "100px" ' . $row_color . '>' . $wrData['id'] .
'</td>
<td width = "150px"' . $row_color . '>' .
companyNameByNameID($wrData['nameid']) .
'('.getKdaccount($wrData['nameid']).')</td>
<td width = "100px"' . $row_color . '>' .
$wrData['orderNumber'] . '</td>
<td width = "600px"' . $row_color . '>' .
$wrData['partNo'] . '</td>
</tr>';
echo '</table><br /><br />';
$i++;
}
}

No comments:

Post a Comment