Run foreach loop inside a msgHTML in PHPMailer - php

I want to run a foreach loop inside a msgHTML on PHP Mailer to make the table td dynamic from array. Below is the latest code I have. I' am working on dreamweaver and it show that this code has errors, I don't know how to solve this issue.
<?php
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$mail->MsgHTML(
"
Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
"
?>
<?php
foreach($variable as $var){
?>
<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
<?php
}
?>
<?php "
</table>
");
?>

How about this code?
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$msg = "Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
foreach($variable as $var){
$msg .= "<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
} // foreach
$msg .= "</table>";
$mail->MsgHTML( $msg );

try this
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$message =
"
Some html and text goes here....
Below is the main part of this topic.
<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
<tr style='background: #003e82; color: #FFF; font-weight: bold;'>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>
";
foreach ($variable as $var) {
$message .= "<tr>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
<td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
</tr>";
}
$message .= "
</table>
";
$mail->MsgHTML($message);

Related

PDF gets breack when printing dompdf

hi I am using dompdf for generating pdf from HTML but not able to understand why it gets break while printing the pdf
here is my function
public function printRunsheetBymanifestAction()
{
$manifestId = $this->params()->fromRoute('id', 0);
$getDetails = $this->getDeliveryTable()->getRunSheetData(array('manifestId' => base64_decode($manifestId)));
$getItemDetails = $this->getManifestTable()->getManifestItemsById($getDetails[0]['manifestId']);
$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('defaultFont','Courier');
$options->set('defaultPaperSize','A4');
$domPdf = new \Dompdf\Dompdf($options);
$domPdf->set_option('isHtml5ParserEnabled', true);
$view = $this->getServiceLocator()->get('ViewRenderer');
$viewModel = new ViewModel();
$viewModel->setTemplate('delivery/index/print-runsheet-bymanifest.phtml');
$viewModel->setVariable('results', $getDetails);
$viewModel->setVariable('itemDetails', $getItemDetails);
$content = $view->render($viewModel); //make html content for loadn in doampdf
$domPdf->loadHtml($content);
$domPdf->render();
$fileName = $getDetails[0]['runsheetNumber'].'_'.date('d_m_Y');
$domPdf->stream($fileName,array("Attachment"=>1));
exit;
}
here is my phtml template even I try the page break CSS for break the page after desired output
<?php $results = $this->results;
$items = $this->itemDetails;
?>
<style>
table.body {
width: 100%;
margin: 0 auto;
}
td.text-right{text-align: right;}
td.text-center{text-align: center;}
.page_breack{
clear: both;
page-break-after: always;
}
</style>
<table class="body" align="center" style="border-top: 1px solid #333; border-left: 1px solid #333; border-right: 1px solid #333;">
<tr>
<td>
<div id="header">
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="width: 30%">
<img style="width: 250px" src="<?=$this->basePath();?>/manifests/generate-barcode/<?php echo $results[0]['runsheetNumber']; ?>">
</td>
<td style="width: 70%">
<table class="row" style="width: 100%">
<tr>
<td >Location: <?php echo $results[0]['branchName']; ?></td>
</tr>
<tr>
<td >Website: www.xfas.in</td>
</tr>
<tr>
<td >
Daily Runsheet for : <?php echo $results[0]['allocated'.$results[0]['allocatedTo'].'Code'].' - '.$results[0]['allocated'.$results[0]['allocatedTo'].'Name']; ?>
</td>
</tr>
<tr>
<td class="text-right">
<strong>Printed # : <?php echo date('d.m.Y'); ?></strong>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table width="100%" style="border-collapse: collapse;">
<tr>
<th style="border-bottom: 2px dashed #333; border-top:2px dashed #333; text-align: center; height: 15px; ">SNo.</th>
<th style="border-bottom: 2px dashed #333; border-top:2px dashed #333; text-align: center; height: 15px; ">AwbNo.</th>
<th style="border-bottom: 2px dashed #333; border-top:2px dashed #333; text-align: center; height: 15px; ">Consignee Details</th>
<th style="border-bottom: 2px dashed #333; border-top:2px dashed #333; text-align: center; height: 15px; ">Pin Code</th>
<th style="border-bottom: 2px dashed #333; border-top:2px dashed #333; text-align: center; height: 15px; ">Signature</th>
</tr>
<?php if(count($items) > 0):
foreach ($items as $key => $val):
// $key = $key +1;
?>
<tr class="<?php
++$key;
if($key%14 == 0 && $key !=0)
echo "page_breack";
//echo ++$key; ?>" ><td style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"><?php echo $key; ?></td>
<td style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"><?php echo $val['Awb']; ?></td>
<td style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"><strong><?php echo $val['Consignee']; ?></strong><br>
<?php echo $val['Address'].', '.$val['Place'].', '.$val['ConsigneeArea']; ?>
</td>
<td style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"><?php echo $val['PinCode']; ?></td>
<td style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"></td>
</tr>
<?php endforeach; ?>
<?php
$ramainRow = count($items)%14;
if($ramainRow >0){
// $cnt = 14 - count($items);
for ($i =0; $i < $ramainRow; $i++){?>
<tr>
<td colspan="5" style="font-size: 12px; text-align:center; height: 65px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;"></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" style="font-size: 12px; text-align:center; height: 53px; border-bottom: 1px solid #333; border-top:1px solid #333; border-collapse: collapse;">
<strong>Receiver Name:</strong> <?php echo $results[0]['allocated'.$results[0]['allocatedTo'].'Code'].' - '.$results[0]['allocated'.$results[0]['allocatedTo'].'Name']; ?><br>
<strong>Signature:</strong>
</td>
</tr>
<?php }?>
<?php endif;?>
</table>
</td>
</tr>
above is the htm fiel file for better understanding i am givig the pdf fil

Codeigniter Dompdf can't remove the black footer.

I am using dompdf in codeigniter, the HTML page looks fine but in dompdf generated pdf, there is a black box.
The code in the footer section is:
<div class="signature-footer">
<table>
<tr>
<td style="text-align: center;border-style:dashed; border-top:1px black;border-bottom:1px solid white; border-left:1px solid white; border-right:1px solid white;">Prepared By<br><?php echo $quotation_detail[0]->employee_name; ?><br><?php echo $quotation_detail[0]->designation_name ; ?></td>
<td style="width: 30%;border-top:1px solid white;border-bottom:1px solid white; border-left:1px solid white; border-right:1px solid white;"> </td>
<td style="text-align: center;border-top:1px solid white;border-bottom:1px solid white; border-left:1px solid white; border-right:1px solid white;"> </td>
<td style="width: 30%;border-top:1px solid white;border-bottom:1px solid white; border-left:1px solid white; border-right:1px solid white;"> </td>
<td style="text-align: center;border-style:dashed; border-top:1px black;border-bottom:1px solid white; border-left:1px solid white; border-right:1px solid white;">Authorized Signature<br> <br> </td>
</tr>
</table>
</div>
</div><div class="below-border-footer"></div><div style="text-align: center"><img src="<?php echo base_url(); ?>interface/img/footerImage.png"> </div></div></body>
</html>
CSS for these two elements:
.below-border-footer {
width: 100%;
border-top: 0 ;
padding-bottom: 0;
text-align: center;
}
.signature-footer {
font-size: 12px;
font-weight: bold;
width: 100%;
position: relative;
margin-top: 70px;
margin-bottom: 2px;
}
I've tried for hours but no result. How can I remove the black box in the footer?

PHP-MYSQL: Not able to fetch multiple record from database into word file

I have to download the records from database into a word file. But m not able to do so as whenever i add while loop around the table it is giving me error.
My code is working fine when i fetch single records.
Can anbody provide me the way to fetch multiple records.
Here is my Code:
<?php
include('dbcon.php');
if($_GET['id']!='')
$id=$_GET['id'];
$party_name=mysql_query("select * FROM tb_party Where party_id='".$id."' ");
$party_name_row=mysql_fetch_array($party_name);
$sample_text1 = $party_name_row['party_name'];
$party_details="select * from tb_party_rate_entry
where party_id='$id'";
$query_details=mysql_query($party_details);
$row_details=mysql_fetch_array($query_details);
$word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
xmlns='http://www.w3.org/TR/REC-html40′";
$word_xml_settings = "<xml><w:WordDocument>
<w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";
$word_landscape_style =
"#page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
} div.Section1{page:Section1;}";
$word_landscape_headinh="{font-size:15px; }";
$word_landscape_div_imf='
<img src="http://silverevents.net/user_image/' .$sample_text1 . '"
height="150" width="150"/>';
$word_landscape_div_start = "<div class='Section1′>";
$word_landscape_div_end = "</div>";
$content = '<html '.$word_xmlns.'>
<head>
<title>Party Rate Report</title>'
.$word_xml_settings.'<style type="text/css">
'.$word_landscape_style.' table,td
{border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
</head>
<body>'.$word_landscape_div_start .
'<table style="width:500px; color:#000000;
margin:0 0 50px 100px; font-family: Arial Black, Gadget,
sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
</tr></table></br>
<table style="width:700px;font-size:15px;font-family:
Arial Black, Gadget, sans-serif;">
<tr>
<td width="180">
Party Name : '. $party_name_row['party_name'] . '
</td>
<td width="250">
Address : '. $party_name_row['party_address'] . '
</td>
<td width="150">
Phone : '. $party_name_row['phone_no']. '
</td>
</tr>
</table><br/>
<table style="width:700px; color:#00000; margin:22px 0 0 0px;
font- family:Helvetica, sans-serif, Verdana;
font-size:15px; border1px solid #666;">
<tr>
<td style=" border:1px solid #666; width:300px;"> Zone</td>
<td style=" border:1px solid #666; width:300px;">
Delivery Mode
</td>
<td style=" border:1px solid #666; width:300px;"> Doc Type </td>
<td style=" border:1px solid #666; width:300px;"> Weight slot </td>
<td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
<td style=" border:1px solid #666; width:300px;"> Flat </td>
<td style=" border:1px solid #666; width:300px;"> Price </td>
</tr>
<tr>
<td style=" border:1px solid #666; width:300px;">' .
$row_details['zone_id']. '</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['delivery_mode_id'].'
</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['doc_type_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['weight_slot_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['weight_measure_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['flat'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['price' ] . '</td>
</tr>
</table></br>' . $word_xml_settings. $word_landscape_div_end.'
</body>
</html>';
#header('Content-Type: application/msword');
#header('Content-Length: '.strlen($content));
#header('Content-disposition: inline; filename="Party Rate Report.doc"');
echo $content;
?>
Here my database:
Quick reformat, adding a loop (and assuming the id is an integer):-
<?php
include('dbcon.php');
if($_GET['id']!='')
{
$id=intval($_GET['id']);
$party_name=mysql_query("select * FROM tb_party Where party_id=$id ");
if ($party_name_row=mysql_fetch_array($party_name))
{
$sample_text1 = $party_name_row['party_name'];
}
$word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
xmlns='http://www.w3.org/TR/REC-html40'";
$word_xml_settings = "<xml><w:WordDocument>
<w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";
$word_landscape_style =
"#page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
} div.Section1{page:Section1;}";
$word_landscape_headinh="{font-size:15px; }";
$word_landscape_div_imf='
<img src="http://silverevents.net/user_image/' .$sample_text1 . '"
height="150" width="150"/>';
$word_landscape_div_start = "<div class='Section1'> ";
$word_landscape_div_end = "</div>";
$content = '<html '.$word_xmlns.'>
<head>
<title>Party Rate Report</title>'
.$word_xml_settings.'<style type="text/css">
'.$word_landscape_style.' table,td
{border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
</head>
<body>'.$word_landscape_div_start .
'<table style="width:500px; color:#000000;
margin:0 0 50px 100px; font-family: Arial Black, Gadget,
sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
</tr></table></br>
<table style="width:700px;font-size:15px;font-family:
Arial Black, Gadget, sans-serif;">
<tr>
<td width="180">
Party Name : '. $party_name_row['party_name'] . '
</td>
<td width="250">
Address : '. $party_name_row['party_address'] . '
</td>
<td width="150">
Phone : '. $party_name_row['phone_no']. '
</td>
</tr>
</table><br/>
<table style="width:700px; color:#00000; margin:22px 0 0 0px;
font- family:Helvetica, sans-serif, Verdana;
font-size:15px; border1px solid #666;">
<tr>
<td style=" border:1px solid #666; width:300px;"> Zone</td>
<td style=" border:1px solid #666; width:300px;">
Delivery Mode
</td>
<td style=" border:1px solid #666; width:300px;"> Doc Type </td>
<td style=" border:1px solid #666; width:300px;"> Weight slot </td>
<td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
<td style=" border:1px solid #666; width:300px;"> Flat </td>
<td style=" border:1px solid #666; width:300px;"> Price </td>
</tr>';
$party_details="select *
from tb_party_rate_entry
where party_id=$id";
$query_details=mysql_query($party_details);
while($row_details=mysql_fetch_array($query_details))
{
$content .= '<tr>
<td style=" border:1px solid #666; width:300px;">' .
$row_details['zone_id']. '</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['delivery_mode_id'].'
</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['doc_type_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['weight_slot_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['weight_measure_id'].'</td>
<td style=" border:1px solid #666; width:300px;">'.
(($row_details['flat'] != '') ? $row_details['flat'] : ' ').'</td>
<td style=" border:1px solid #666; width:300px;">'.
$row_details['price' ] . '</td>
</tr>';
}
$content .= '</table></br>' . $word_xml_settings. $word_landscape_div_end.'
</body>
</html>';
#header('Content-Type: application/msword');
#header('Content-Length: '.strlen($content));
#header('Content-disposition: inline; filename="Party Rate Report.doc"');
echo $content;
}
?>
Note for new code you probably should switch to using mysqli rather than the php mysql drivers
do something like this:
while ($row_details=mysql_fetch_array($query_details)) {
//do row specific code
}

Creating new table rows with data using php

I have a table that is populated with php via a text file. I know how to create a new table for each new data entry but I am trying to just create a new row instead of a whole new table. How can I create a new row with data instead of an entire table?
This is currently what I have now and I just want to create a new row with the variables in it. How can I achive this without it creating a whole new table each time. I have tried a couple of things with no success.
$tracreport = $tracreport . "<table width='100%' cellpadding='0' cellspacing='0' style='margin-top:15px; background-color:#000; color:#FFF;'>
<tr>
<td style='padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;'> Tracking " . $tnumber[1] . " active storm cell</td>
</tr>
<tr>
<td>
<table width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td>
<table id='tracReport' width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Cell ID</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Tracking Since</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Bearing</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Distance</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Intensity</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Trend</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Last Strike</td
</tr>
<tr>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tid[$k] . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdtime[$k] . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdirection[$k] . " °</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdistance[$k] . " miles</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . strtolower($tintensity[$k]) . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . trim(strtolower($ttrend[$k])) . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . trim(strtolower($tactivity[$k])) . "</td>
</tr>
</table>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>";
You could try splitting your table, and add your loop in the middle
// begin your table
$tracreport = $tracreport . "<table width='100%' cellpadding='0' cellspacing='0' style='margin-top:15px; background-color:#000; color:#FFF;'>
<tr>
<td style='padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;'> Tracking " . $tnumber[1] . " active storm cell</td>
</tr>
<tr>
<td>
<table width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td>
<table id='tracReport' width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Cell ID</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Tracking Since</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Bearing</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Distance</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Intensity</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Trend</td>
<td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Last Strike</td
</tr>"; // break the top part of the table
// create your loop
for($k=0;$k<YOUR MAX ROWS;$k++){
$tracreport .="<tr>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tid[$k] . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdtime[$k] . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdirection[$k] . " °</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdistance[$k] . " miles</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . strtolower($tintensity[$k]) . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . trim(strtolower($ttrend[$k])) . "</td>
<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . trim(strtolower($tactivity[$k])) . "</td>
</tr>";
} // close your loop
// finish your table
$tracreport .= "</table>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>";
edit
instead of for($k=0;$k<YOUR MAX ROWS;$k++){...}, if you don't know the total max, use a foreach loop
foreach($tid as $k => $v){
...
}
this will then loop through each of the values without needing to know the max count
Use this method to generate table rows:
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Name</td>
<td>Age</td>
</tr>
<?php
$yourquery = mysql_query("");
while ($rows = mysql_fetch_assoc($yourquery)) {
?>
<tr>
<td><?php $rows['name']; ?></td>
<td><?php $rows['age']; ?></td>
</tr>
<?php } ?>
</table>

TCPDF split html table on multiple pages

I use TCPDF for outputting an html table, this one in my example is not the only one that i would have to modify so i need some rules for my pages to be rendered.
So far TCPDF seemed to me a powerfull library so i would appreciate not to try another one.
My issue is that i need to add the table header to every new page the table will appear to, but i have also other info to write to this pdf, that must not have the table header so i cannot use the rewrite of header method in tcpdf.
<table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" ><tr><td><table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" ><tr><td class="TableHeader" width="5%" style="padding: 5px; border-top: 1px solid #000000; border-left: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Nr.</td><td class="TableHeader" width="40%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Denumire</td><td class="TableHeader" width="20%" align="left" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Cod</td><td class="TableHeader" width="10%" align="center" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Cant.</td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Pret unitar<br />fara TVA (RON)</td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-bottom: 1px solid #E4E9EC; border-top: 1px solid #000000; border-right: 1px solid #000000;" >Pret total<br />fara TVA (RON)</td></tr></table></td></tr></table><table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" id="Container"><tr><td><table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" ><tr><td class="TableHeader" width="5%" style="padding: 5px; border-left: 1px solid #000000;" > </td><td class="TableHeader" width="40%" style=" padding: 5px;" > </td><td class="TableHeader" width="20%" style=" padding: 5px;" > </td><td class="TableHeader" width="10%" style=" padding: 5px;" > </td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px;" > </td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-right: 1px solid #000000;" > </td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; ">1</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC;"><b>sectiune1</b></td><td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000;"> </td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">1.1</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">UNI-Cazan mixt AIREX 25 2S BICOMB MODUL </td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">94456 </td><td class="TableRow" align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">24.623,00</td><td class="TableRow" align="right" style="padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">2.462.275.377,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">1.2</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">UNI-Cazan mixt AIREX 25 2S BICOMB MODUL </td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">94456 </td><td class="TableRow" align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">24.623,00</td><td class="TableRow" align="right" style="padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">2.462.275.377,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; "> </td><td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "><b> TOTAL sectiune1:</b></td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> </td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> </td><td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "><b>4.924.550.754,00</b></td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "> </td><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;"> </td><td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "> </td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; ">2</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-top: 1px solid #000;"><b>sectiune2</b></td><td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000; border-top: 1px solid #000;"> </td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">2.1</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">ICI-Cazan condensatie MONOLITE 75-JB</td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">81020076 </td><td class="TableRow" align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">34.719,00</td><td class="TableRow" align="right" style="padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">3.471.865.281,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; "> </td><td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "><b> TOTAL sectiune2:</b></td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> </td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> </td><td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "><b>3.471.865.281,00</b></td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "> </td><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;"> </td><td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "> </td></tr><tr><td class="TableTotal" style=" padding: 5px; border-left: 1px solid #000000;"></td><td colspan="3" class="TableTotal" style=" padding: 5px; " ><b>Total sectiune1:</b></td><td align="right" class="TableTotal" style=" padding: 5px; " ><b> </b></td><td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" ><b>4.924.550.754,00 </b></td></tr><tr><td class="TableTotal" style=" padding: 5px; border-left: 1px solid #000000;"></td><td colspan="3" class="TableTotal" style=" padding: 5px; " ><b>Total sectiune2:</b></td><td align="right" class="TableTotal" style=" padding: 5px; " ><b> </b></td><td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" ><b>3.471.865.281,00 </b></td></tr><tr><td class="TableTotal" style=" padding: 5px; border-left: 1px solid #000000;border-top: 1px solid #E4E9EC; "></td><td colspan="4" class="TableTotal" style=" padding: 5px;border-top: 1px solid #E4E9EC; " ><b>Total oferta (RON):</b></td><td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-top: 1px solid #E4E9EC; " ><b>8.396.416.035,00 </b></td></tr><tr><td class="TableTotal" style=" padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #000000;"></td><td colspan="4" class="TableTotal" style=" padding: 5px;border-bottom: 1px solid #000000;" ><b>Total oferta (RON cu TVA inclus):</b></td><td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #000000;" ><b>10.411.555.883,40 </b></td></tr></table></td></tr></table>
I thought of the following solution: if somehow i could split this html to rows and write row by row to pdf so that i can rollback when i write over the footer and then i add a new page, but there is a major problem, i get all sorts of warnings because the html table tags are not closed.
Here is my code:
$pdf->setAutoPageBreak(false);
$pdf->startTransaction();
$html = new simple_html_dom();
$html->load($data);
$single = $html->find('#Container', 0);
if($single){
$rows = $single->getElementsByTagName('tr');
$rows = $rows[0]->getElementsByTagName('tr');
if($rows)
for($i=1;$i<(count($rows)-1);$i++){
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '<table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]->outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
if ($pdf->getY() > $pdf->getPageHeight() - 30) {
$pdf->rollbackTransaction(true);
$pdf->AddPage();
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '<table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]->outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
}
}
}
$pdf->commitTransaction();
$pdf->setAutoPageBreak(true, 30);
I took a quick look at this 2 articles, maybe someone could find this handy:
Some paging with multicell
and Another paging with multicell . I found this 2 worthy of an example as a solution logic, but the methods used with tcpdf seemed to me a little weird.
I think your solution is a good one. Only other method I can think of is to use fixed width multicells which can be quite a pain but gives you static column widths.
I noticed a few problems however:
Your transaction wraps the entire document, a rollback should reset the document to nothing.
Your warnings are likely caused by the fact that you are passing partial html code which is not render-able to writeHTMLCell. (You pass just <table> or </table> which when alone, cannot be rendered)
Your columns wouldn't line up if you wrapped each row in table tags. The best bet is to wrap the rows on that page in a table block.
Try these changes and let me know how it goes
$pdf->setAutoPageBreak(false);
//$pdf->startTransaction(); // Moved
$html = new simple_html_dom();
$html->load($data);
$single = $html->find('#Container', 0);
if($single){
$rows = $single->getElementsByTagName('tr');
$rows = $rows[0]->getElementsByTagName('tr');
if($rows) {
$pdf->startTransaction(); // Start transaction only because we may need it
// Header for html, this starts the html and can optionally insert the header row as the first row on every new page.
$html_header = '<tr><td>Name</td><td>Age</td></tr>';
$html_buffer = '<table>'.$html_header;
for($i=1;$i<(count($rows)-1);$i++){
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.$rows[$i]->outertext.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
if ($pdf->getY() < ($pdf->getPageHeight() - 30)) { // Note the less-than operator
// We might be able to add some more text, so undo that
$pdf->rollbackTransaction(true);
// And store the html
$html_buffer .= $rows[$i]->outertext;
}else{
// We exceeded our limit
$pdf->rollbackTransaction(true);
// Write last known good table
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
// Add a new page
$pdf->AddPage();
// End this transaction
$pdf->commitTransaction();
// Start a new transaction
$pdf->startTransaction();
// Reset html buffer
$html_buffer = '<table>'.$html_header;
// Add line we couldn't fit on last page to html buffer
$html_buffer .= $rows[$i]->outertext;
}
}
// There is still information in our buffer and it fits on a single page
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
// Final commit
$pdf->commitTransaction();
}
}
$pdf->setAutoPageBreak(true, 30);
This wraps everything on the page in a table block so the columns will line up on that page. (They may still be different from page to page however) Also, I gave you the ability to add your own header to each new page like you wanted. Just change $html_header to your own header row code.
You are right when you said TCPDF can be confusing but it is also extremely powerful and once you get the hang of it you can make some very nice documents.
Go to TCPDF and choose Example 48, you will see an example with <thead> element.

Categories