简体   繁体   中英

How to sum each column in MySQL table with PHP

I want to display the total of each column inside the table. Here is my code.

$Tab_info['Width'] = "600" ;
$Col_det = array(
    'Name' => array( 'Name',
                    'Aging Create Order ( <= 5 )',
                    'Aging Create Order ( > 5 )',
                    'Total'),
    'Size' => array( '200' ,  '200' ,  '200' ,  '100'  ),
    'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-'));

$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">';
$Delim_String .= '<thead><tr>'; 
                    for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>';
}

$Delim_String .= '</tr>
  </thead>
  <tbody>
    '; 
//===================Summary Aging Create Order End=========================== 

$vSQL = "SELECT u.Nama as Name,
         SUM( CASE WHEN t.`AgingCreateOrder` <= 5 THEN 1 ELSE 0 END) 'Aging Less Than or Equal 5', 
         SUM( CASE WHEN t.`AgingCreateOrder` > 5 THEN 1 ELSE 0 END) 'Aging More Than 5',
         COUNT(*) as Total
            FROM `oct_usergroup` u
                JOIN `oct_tickets` t 
                ON (t.`Creator_SubType`= u.`Id`)
            WHERE
                t.Date_Created >= (CURDATE() -INTERVAL 49 DAY) 
                AND u.Nama IS NOT NULL
                AND t.AgingCreateOrder between '1' and '1999'
                AND t.Status = 1
            GROUP BY u.`Nama`";                                 

$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());
while($row = mysql_fetch_array ($result))
    {           
$Delim_String .= '
        <tr>
            <td>'.$row['Name'].'</td>
            <td>'.$row['Aging Less Than or Equal 5'].'</td>
            <td>'.$row['Aging More Than 5'].'</td>
            <td>'.$row['Total'].'</td>
        </tr>';
    }

$Delim_String .= "</tbody></table>";
$table1 = $Delim_String;
unset($Delim_String);

//===================Summary Aging Update Address start===========================
$Tab_info['Width'] = "600" ;
$Col_det = array(
    'Name' => array( 'Name',
                    'Aging Update Address ( <= 5 )',
                    'Aging Update Address ( > 5 )',
                    'Total'),
    'Size' => array( '200' ,  '200' ,  '200' ,  '100'  ),
    'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-')
    );

$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">
';
$Delim_String .= '<thead>
    <tr>
    '; 
for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '
<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>
';
}

$Delim_String .= '</tr>
  </thead>
  <tbody>
    ';

I want it to show like this.

====================================================================
| Name     | Aging Create Order <=5 | Aging Create Order >5 | Total |
---------------------------------------------------------------------
| Consumer | 159                    | 80                    | 239   |
---------------------------------------------------------------------
| CSO      | 20                     | 50                    | 70    |
---------------------------------------------------------------------
| Total    | 179                    | 130                   | 309   |
=====================================================================

The last row is the one to be added.

Try with this

$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());

$gt_less = 0;
$gt_more = 0;
$gt_total = 0;
while($row = mysql_fetch_array ($result))
{  
    $gt_less += $row['Aging Less Than or Equal 5']; 
    $gt_more += $row['Aging More Than 5'];
    $gt_total += $row['Total'];       
    $Delim_String .= '
    <tr>
        <td>'.$row['Name'].'</td>
        <td>'.$row['Aging Less Than or Equal 5'].'</td>
        <td>'.$row['Aging More Than 5'].'</td>
        <td>'.$row['Total'].'</td>
    </tr>';
 }
 $Delim_String .= '
    <tr>
        <td>Grand Total</td>
        <td>'.$gt_less.'</td>
        <td>'.$gt_more.'</td>
        <td>'.$gt_total.'</td>
    </tr>';

$Delim_String .= "</tbody></table>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM