Home » Php » php – How to calculate the SUM of rows values for one column of a table created using TCPDF?

php – How to calculate the SUM of rows values for one column of a table created using TCPDF?

Posted by: admin February 25, 2020 Leave a comment

Questions:

Please could you help me to calculate and display the SUM VALUE of the rows of a column named “prime” in my table which is created in a PDF report using TCPDF (the data is being extracted from my database).
Thank you in advance. I would be very grateful if you could help me.
A better description of my result table in this link
The main part of my code is this:

    <?php
set_time_limit(0);
tcpdf();


 // create new PDF document
$pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
//$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';

// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);

// ---------------------------------------------------------



// add a page
$pdf->AddPage();


$pdf->SetXY(16, 10);


// set document information
$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('Nicola Asuni');
//$pdf->SetSubject('TCPDF Tutorial');
//$pdf->SetKeywords('TCPDF, PDF, example, navettes, guide');
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.'', PDF_HEADER_STRING);
 $pdf->setPrintHeader(false);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));



$pdf->setRTL(true);

$pdf->SetFont('xzar', '', 10);

    $sql= "SELECT * FROM nav_agent WHERE matricule='$mat'";  
    $query = $this->db->query($sql); 


$saut=0;
$tbl = '<table cellspacing="0" cellpadding="1" border="1">
   <thead>
        <tr>
            <th style="border: 1px double #000000; text-align:center;"  colspan="2">الإذن </th>
            <th style="border: 1px double #000000; text-align:center;"  colspan="2">التنقل</th>
            <th style="border: 1px double #000000; text-align:center;"  colspan="2">التوقيت</th>
            <th style="border: 1px double #000000; text-align:center;"  colspan="4"> </th>
       </tr>


<tr>
<td style="border: 1px double #000000; text-align:center;">العدد</td>  
<td style="border: 1px double #000000; text-align:center;">التاريخ</td>  
<td style="border: 1px double #000000; text-align:center;">من</td>  
<td style="border: 1px double #000000; text-align:center;">إلى</td>  
<td style="border: 1px double #000000; text-align:center;">الإنطلاق</td>  
<td style="border: 1px double #000000; text-align:center;">الرجوع</td>  
<td style="border: 1px double #000000; text-align:center;">المدة المقضاة (بالساعة)</td>  
<td style="border: 1px double #000000; text-align:center;"> مبلغ منحة التنقل </td> 
<td style="border: 1px double #000000; text-align:center;">رقم السيارة</td>
</tr>
';

$prime=0;
foreach ($query->result() as $row){

 $sql1 = "SELECT * FROM fiche_nav WHERE id_navette='$row->id_navette'";  


  $query1 = $this->db->query($sql1);    
foreach ($query1->result() as $row1){


$delai = strtotime($row1->heureret) - strtotime($row1->heuredep); 
$time_h = ($delai/60)/60; 


if ($val_grade == 'A1' or   $val_grade == 'A2')
    $montant1 = 15.000;
    else
        if ($val_grade == 'A3' or   $val_grade == 'B'  
         or     $val_grade =='ouv cat 8' or $val_grade =='ouv cat 9' or $val_grade =='ouv cat 10')  


    $montant1 = 9.000;
    else
        if ($val_grade == 'C' or    $val_grade == 'D'  
         or     $val_grade =='ouv cat 1' or $val_grade =='ouv cat 2' or $val_grade =='ouv cat 3'    
         or $val_grade =='ouv cat 4' or $val_grade =='ouv cat 5' or $val_grade =='ouv cat 6' or $val_grade =='ouv cat 7')

    $montant1 = 6.750;   


if ($val_grade == 'A1' or   $val_grade == 'A2')
    $montant2 = 35.000;
    else
        if ($val_grade == 'A3' or   $val_grade == 'B'  
         or     $val_grade =='ouv cat 8' or $val_grade =='ouv cat 9' or $val_grade =='ouv cat 10')  


    $montant2 = 25.000;
    else
        if ($val_grade == 'C' or    $val_grade == 'D'  
         or     $val_grade =='ouv cat 1' or $val_grade =='ouv cat 2' or $val_grade =='ouv cat 3'    
         or $val_grade =='ouv cat 4' or $val_grade =='ouv cat 5' or $val_grade =='ouv cat 6' or $val_grade =='ouv cat 7')

    $montant2 = 18.000;


if ( $time_h <= 7 )
$prime = 0;
else

    if ( $time_h >7 && $time_h <= 14 ) 
$prime = 0.5 * $montant1;

else


    if ( $time_h >14 && $time_h < 24 )
$prime = $montant1; 


else


    if ( $time_h >=24 ) 
$prime = $montant2;     

$tbl=$tbl. '<tr>
<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->datedep .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->lieudep .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->destination .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->heuredep .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->heureret .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $time_h .'</td>
<td style="border: 0px solid #000000; text-align:center">'. $prime .'</td>
<td style="border: 0px solid #000000; text-align:center;"> '.$row1->matriculevoiture .' </td>

<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center"></td>
</tr>
' ;

}


}

$tbl=$tbl.' </thead></table> ';


$pdf->writeHTML($tbl, true, false, false, false, '');


  // WHAT SHALL I ADD HERE TO CALCULATE the TOTAL VALUE i.e SUM OF "$PRIME" ROWS?

// close and output PDF document
 $date_auj=date('dmY',NOW());
    $strr = "Rapport-".$date_auj.".pdf";
$pdf->Output($strr, 'I');
//ob_end_clean(); 
//$pdf->Output($strr, 'I');

//============================================================+
// END OF FILE
//============================================================+
?>
How to&Answers:

you can declare a variable before $prime variable

$grandPrime=0;
$prime=0;
foreach ($query->result() as $row){

initialize it here

$grandPrime+=$prime;
$tbl=$tbl. '<tr>
<td style="border: 0px solid #000000; text-align:center"></td>
<td style="border: 0px solid #000000; text-align:center">'. $row1->datedep .'</td>

and use it after your loop end.

Answer:

Thank you dear members for you precious help! Indeed, another sum variable was missing.
I resolved the problem by adding it as shown below:

$grandPrime=0; $prime=0; 

foreach ($query->result() as $row)

{ //MY IF CONDITIONS TO CALCULATE THE VALUE OF PRIME etc ..

$grandPrime+=$prime; 

$tbl=$tbl. '<tr> 

//MY TABLE CONTENT..etc.

</tr> ' ; } 
$pdf->writeHTML($tbl, true, false, false, false, ''); 

// THEN I ADDED A FOR LOOP TO DISPLAY grandPRIME VALUE, where $num_prime is simply the number of prime rows

$num_prime = count($prime);
 for($i = 0; $i < $num_prime; ++$i) 
{ $pdf->SetFont('xzar', 'B', 11);
 $pdf->Cell(277,5, " المبلغ الجملي $grandPrime ",1,0,'C'); 
} 

This is the correct result I finally obtained: Please clic the link to see the picture

THANK YOU AGAIN :)) YOU REALLY HELPED ME !

Answer:

After the line

$prime=0;

Add another var let’s name it $sumPrimes

$sumPrimes =0;

And after your last else when you assign your $prime:

else
    if ( $time_h >=24 ) 
$prime = $montant2;

You could calculate the sum like that:

$sumPrimes += $prime;

After your foreach and before you close the table:

$tbl=$tbl.' </thead></table> ';

You can print the $sumPrimes.