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

Related searches

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
//============================================================+
?>

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.

Examples � TCPDF, Example 001 : first example with default Header and Footer � Example 002 : without Example 010 : text on multiple columns � Example 011 : table with primitive� When you have a data table in your worksheet you can insert the Total Row option for summing up the data in a table. For inserting the Total Row, first, select any cell of the table and in the Design tab, select the Total Row under the Table Style option. You will see the sum value of column D after selecting the Total Row option. It means the total sales for March will be seen with this.


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 !

voilab/tctable: Create quickly and efficiently TCPDF tables , Create quickly and efficiently TCPDF tables with advanced page break options The goal of this library is not to replace a complex HTML table with row and cell spans. Useful when you have lot of lines, and a total as the // last one. Plugin { // column on which we sum its value private $column; // the current calculated� In the opening Combine Rows Based on Column dialog box, you need to: (1) Select the column name that you will sum based on, and then click the Primary Keybutton; (2) Select the column name that you will sum, and then click the Calculate> Sum. (3) Click the Ok button.


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.

How to sum values from table column and update when remove or , table column and update when remove or add new row in jQuery with demo Create a new row element */ var idNum = ($tableBody.find('tr').length + 1); var� To total values in other columns, simply select a corresponding cell in the total row, click the drop-down list arrow, and select Sum: If you want to perform some other calculation, select the corresponding function from the drop-down list such as Average, Count, Max, Min, etc.


Sum Total for Column in jQuery, Sum Total for Column in jQuery - Learn How Sum Total for Column in jQuery with jQuery Sum values When Update Row jQuery Multiple select box value in a src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> calculateColumn(index) { var total = 0; $('table tr').each(function() { var value� 1. Select a blank cell, enter formula= SUM(OFFSET(Score_range,1,0,1,COLUMNS(Score_range)))into the Formula Bar and then press the Enterkey. See screenshot: Notes: 1. In the formula, Score_rangeis the name of the named range you want to sum certain row inside. 2.


How to output mysql table content to pdf using tcpdf? - PHP, I need to create pdf document which outputs all rows from mysql table, below are the attached code <div sadock016 June 19, 2017, 8:59pm #1. I need to create </label><br/> <label>Total Amount Retail Price is <?php amounttotalretail(); ?> If you see tutorials and sample code that uses them, then find a newer tutorial. Sum values by group with using formula. You can sum values by group with one formula easily in Excel. Select next cell to the data range, type this =IF(A2=A1,"",SUMIF(A:A,A2,B:B)), (A2 is the relative cell you want to sum based on, A1 is the column header, A:A is the column you want to sum based on, the B:B is the column you want to sum the values.)


For more information about using a Total row, see the article Display column totals in a datasheet. Top of Page. Calculate grand totals by using a query. A grand total is the sum of all the values in a column. You can calculate several types of grand totals, including: A simple grand total that sums the values in a single column.