1

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!

1
  • 1
    One idea, create a variable and start adding the row value and print the total. Commented Mar 29, 2017 at 18:15

1 Answer 1

1
$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>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I knew there was a simple solution that I was missing - this worked perfectly. I was overthinking it!
Ok, this got more complicated - I now need to add weekly totals in the table in addition to the single day numbers and running count. I have tried to figure this out with break/continue in the php foreach loop and that just breaks the whole table, and I've tried to figure out how to do it with javascript but I can't even figure out how to get the sum of one column in the table let alone breaking it out by week. Any help would be greatly appreciated!
Create a new question, You can reference this one. But provide one example with data and expected output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.