Reputation: 13
I am trying to create a table that displays member data for three different types of members - it should display how many members were added each day, and then there needs to be a column that keeps a cumulative total month to date for the three different types of members. Example:
Members Added in March:
Date Type A Type A Total Type B Type B Total
1 23 23 16 16
2 13 36 13 29
3 16 52 17 46
My query looks like this:
$results=$wpdb->get_results("SELECT DAY(Subscription_Start_Date) as dayOfMonth, COUNT(a.user_id) as typeA, COUNT(b.user_id) as typeB, COUNT(c.user_id) as typeC
FROM users u
LEFT OUTER JOIN typeAUser a ON u.type_id = a.User_Id
LEFT OUTER JOIN typeBUser b ON u.type_id = b.User_Id
LEFT OUTER JOIN typeCUser c ON u.type_id = c.user_Id AND c.phone_opt_out != '1'
WHERE MONTH(Subscription_Start_Date) = MONTH(curdate()) AND YEAR(Subscription_Start_Date) = YEAR(curdate())
GROUP BY DAY(Subscription_Start_Date)
ORDER BY DAY(Subscription_Start_Date)");
I am outputting the data into a table using a foreach loop like so:
$resultTable = "<table><thead>
<tr><td colspan='7'>Month to Date Members Added $currentMonth</td></tr>
<tr>
<th>Day of the Month</th>
<th>Type A</th>
<th>Type A Month to Date</th>
<th>Type B</th>
<th>Type B Month to Date</th>
<th>Type C</th>
<th>Type C Month to Date</th>
</tr></thead><tbody>";
foreach($results as $r){
$aMemPerDay = $r->typeA;
$bMemPerDay = $r->typeB;
$cMemPerDay = $r->typeC;
$resultTable .= "<tr>
<td>$r->dayOfMonth</td>
<td>$aMemPerDay</td>
<td>???</td>
<td>$bMemPerDay</td>
<td>???</td>
<td>$cMemPerDay</td>
<td>???</td>
</tr>";
}
$resultTable .="</tbody></table>";
echo $resultTable;
The query returns the info I need for the number of members added per day, but I can't figure out how to keep a running total that increase each day. I have tried many things, but nothing seems to work. I tried implementing a variation of this solution within the foreach on each of the member types but when I put the $totalA variable into the table, it just printed out Array:
$totalA = array();
$runningSumA = 0;
foreach ($aMemPerDay as $aMem) {
$runningSumA += $aMem;
$totalA[] = $runningSum;
}
I even tried using a MySQL variable in the query (since there are only 30 days per month, I figured it wouldn't be overly cumbersome), but I wasn't able to get a cumulative result there either. I know that there is probably some manageable answer out there, but I can't seem to come up with it. Any direction would be appreciated!
Upvotes: 1
Views: 1363
Reputation: 48197
$TotalA = 0;
$TotalB = 0;
$TotalC = 0;
foreach($results as $r){
$aMemPerDay = $r->typeA;
$bMemPerDay = $r->typeB;
$cMemPerDay = $r->typeC;
$TotalA += $aMemPerDay;
$TotalB += $bMemPerDay;
$TotalC += $bMemPerDay;
$resultTable .= "<tr>
<td>$r->dayOfMonth</td>
<td>$aMemPerDay</td>
<td>???</td>
<td>$bMemPerDay</td>
<td>???</td>
<td>$cMemPerDay</td>
<td>???</td>
</tr>";
}
Upvotes: 1