[英]TCPDF how to write the PHP code inside the HTML
我正在使用 TCPDF 創建庫存清單打印。 我在將 while 循環放入 HTML 標簽時遇到了一個問題,它出錯了,我該如何正確編寫它。 我嘗試過所以我寫在下面。 日期和lastinserted和grandtotal工資余額都顯示得很好。 只有我需要while循環我試過很多次但我不能
<?php
require_once('lib/master/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, [80, 230], true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
include("db.php");
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$last_insert_id = 51;
$sql = "select i.sales_id,i.product_id,i.sell_price,i.qty,i.discount,i.total,s.date,s.grand_total,s.pay,s.balance,pr.p_name from sales_product i, sales s,product pr where s.id = i.sales_id and i.product_id = pr.barcode and i.sales_id = $last_insert_id ";
$orderResult = $conn->query($sql);
$orderData = $orderResult->fetch_array();
$sales_id = $orderData[0];
$product_id = $orderData[1];
$price = $orderData[2];
$qty = $orderData[3];
$discount = $orderData[4];
$total = $orderData[5];
$date = $orderData[6];
$grand_total = $orderData[7];
$pay = $orderData[8];
$balance = $orderData[9];
$product_name = $orderData[10];
// create some HTML content
$html = '<p align="center"><b style="font-size: 14px">Tutus Funny</b><br>
<i>Computer best lessons online</i>
</p>
<p align="left">
Order # '.$last_insert_id.' </b>
<br>
Date # <b>'.$date.' </b>
</p>
<table class="table table-condensed">
<thead>
<tr>
<td align="center"><strong>Pname</strong></td>
<td align="center"><strong>Qty</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Total</strong></td>
</tr>
</thead>
'.
$x = 1;
$last_insert_id = $_GET['last_id']; //
$orderResult = $conn->query($sql);
while($row = $orderResult->fetch_array()){
?>
<tr>
<td align="center">.$pname.</td>
<td align="center">.$price.</td>
<td align="center">.$qty.</td>
<td align="center">.$total.</td>
</tr>
$x++; } .'
</table>
<br>
<div align="right">
Sub Total <b>'.$grand_total.'</b><br>
Pay <b> '.$pay.'</b><br>
Due <b>'.$balance.'</b>
</div>
<div align="center">
<i>60 b bank road badulla</i>
</div>
</div>
</div>
';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// reset pointer to the last page
$pdf->lastPage();
$pdf->Output();
}
?>
試試這個代碼
<?php
require_once('lib/master/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, [80, 230], true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
include("db.php");
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$last_insert_id = 51;
$sql = "select i.sales_id,i.product_id,i.sell_price,i.qty,i.discount,i.total,s.date,s.grand_total,s.pay,s.balance,pr.p_name from sales_product i, sales s,product pr where s.id = i.sales_id and i.product_id = pr.barcode and i.sales_id = $last_insert_id ";
$orderResult = $conn->query($sql);
$orderData = $orderResult->fetch_array();
$sales_id = $orderData[0];
$product_id = $orderData[1];
$price = $orderData[2];
$qty = $orderData[3];
$discount = $orderData[4];
$total = $orderData[5];
$date = $orderData[6];
$grand_total = $orderData[7];
$pay = $orderData[8];
$balance = $orderData[9];
$product_name = $orderData[10];
// create some HTML content
$html = '<p align="center"><b style="font-size: 14px">Tutus Funny</b><br><i>Computer best lessons online</i></p><p align="left">Order # '.$last_insert_id.' </b><br>Date # <b>'.$date.' </b>
</p><table class="table table-condensed"><thead>
<tr><td align="center"><strong>Pname</strong></td>
<td align="center"><strong>Qty</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Total</strong></td>
</tr>
</thead>';
$x = 1;
$last_insert_id = $_GET['last_id']; //
$orderResult = $conn->query($sql);
while($row = $orderResult->fetch_array())
{
$html.='<tr>
<td align="center">'.$pname.'</td>
<td align="center">'.$price.'</td>
<td align="center">'.$qty.'</td>
<td align="center">'.$total.'</td>
</tr>';
$x++; }
$html.='</table>
<br>
<div align="right">
Sub Total <b>'.$grand_total.'</b><br>
Pay <b> '.$pay.'</b><br>
Due <b>'.$balance.'</b>
</div>
<div align="center">
<i>60 b bank road badulla</i>
</div>
</div>
</div>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// reset pointer to the last page
$pdf->lastPage();
$pdf->Output();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.