I just wanted to get any quick opinions on how i'm handling a count. Originally I thought I could do everything in one query, but if you can I can't figure it out. I want to get the total orders for each month separately and this seems to do the trick.
I've asked before if a query in a loop is bad and I think it traditionally is, but here is an example of what I'm doing. All comments are appreciated.
$query1 = "SELECT orderinfo_date, ROUND(SUM(orderitems_price*orderitems_quantity),2) as monthSum, date_format(`orderinfo_date`, '%m') AS monthId
FROM orderinfo
LEFT JOIN orderitems
ON orderinfo.orderinfo_id = orderitems.orderitems_oid
WHERE year(orderinfo_date) = $yearId
GROUP BY month(orderinfo_date)";
$result1 = mysql_query($query1);
Some html goes in here that builds the page.
while($row1 = mysql_fetch_assoc($result1)) {
extract($row1);
$orderinfo_date = strtotime($orderinfo_date);
$orderMonth = date('F', $orderinfo_date);
$monthId = date('m', $orderinfo_date);
$totalSum[] = $monthSum;
$query2 = "SELECT COUNT(orderinfo_id) AS totalNum
FROM orderinfo
WHERE date_format(`orderinfo_date`, '%m') = '$monthId' AND date_format(`orderinfo_date`, '%Y') = '$yearId' AND orderinfo_status = '2'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_assoc($result2);
$totalOrders = $row2['totalNum'];
<tr>
<td class="details"><a href="<?php htmlentities($_SERVER['PHP_SELF']);?>index.php?page=company&monthId=<?php echo $monthId; ?>&yearId=<?php echo $yearId; ?>"><?php echo strtoupper($orderMonth); ?></a></td>
<td><?php echo $totalOrders; ?></td>
<td>$<?php echo $monthSum; ?></td>
</tr>
}