英文:
error message in coverting php html form in pdf using dompdf
问题
我尝试在单击提交按钮后使用dompdf将我的PHP/HTML表单转换为PDF。一切都正常工作,但当我使用for循环时,出现以下错误:
解析错误:语法错误,意外的 'servproname' (T_STRING),期望 ',' 或 ')' 在 C:\xampp\htdocs\abc\invoice.php
这是我的PHP代码
<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
if(isset($_POST['submit']))
{
$dompdf = new Dompdf();
$dompdf->loadHtml('
<html>
for ($i=0;$i<count($_POST['servproname']);$i++) {
<table>
<tr>
<td style="text-align:left; border:1px solid #cccccc;">'.$_POST['servproname'][$i].'</td>
<td style="text-align:left; border:1px solid #cccccc;">'.$_POST['docname'][$i].'</td>
<td style="text-align:center; border:1px solid #cccccc;">'.$_POST['price1'][$i].'</td>
<td style="text-align:center; border:1px solid #cccccc;">'.$_POST['qty'][$i].'</td>
<td style="text-align:center; border:1px solid #cccccc;">'.$_POST['tot4'][$i].'</td>
</tr>
</table>
}
</html>');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("",array("Attachment" => false));
exit(0);
}
?>
英文:
i am trying to convert my PHP/HTML form into pdf by using dompdf after clicking submit button. Everything was working fine but when I used for loop then following error occuered :
> Parse error: syntax error, unexpected 'servproname' (T_STRING), expecting ',' or ')' in C:\xampp\htdocs\abc\invoice.php
here is my php code
<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
if(isset($_POST['submit']))
{
$dompdf = new Dompdf();
$dompdf->loadHtml('
<html>
for ($i=0;$i<count($_POST['servproname']);$i++) {
<table>
<tr>
<td style = "text-align:left; border:1px solid #cccccc;">'.$_POST['servproname'][$i].'</td>
<td style = "text-align:left; border:1px solid #cccccc;">'.$_POST['docname'][$i].'</td>
<td style = "text-align:center; border:1px solid #cccccc;">'.$_POST['price1'][$i].'</td>
<td style = "text-align:center; border:1px solid #cccccc;">'.$_POST['qty'][$i].'</td>
<td style = "text-align:center; border:1px solid #cccccc;">'.$_POST['tot4'][$i].'</td>
</tr>
}
</table>
');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("",array("Attachment" => false));
exit(0);
}
?>
答案1
得分: 3
你的语法使用方式完全错误,将PHP嵌入字符串中?当你使用单引号(')或双引号(")时,PHP会将其视为字符串而不是要解释的代码。
你应该这样做:
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
if (isset($_POST['submit'])) {
$dompdf = new Dompdf();
$tables = '';
for ($i = 0; $i < count($_POST['servproname']); $i++) {
$tables .= "<table>
<tr>
<td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['servproname'][$i]}</td>
<td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['docname'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['price1'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['qty'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['tot4'][$i]}</td>
</tr>
</table>";
}
$dompdf->loadHtml("<html> $tables </html>");
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("", array("Attachment" => false));
exit(0);
}
正如你所看到的,我创建了一个名为**$tables**的变量,它是你想要生成的字符串。
英文:
The way you are using syntax is totally wrong, using PHP in strings? when you open a ' or ", PHP recognizes that as a string not a code to interpret.
You should do something like this:
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
if (isset($_POST['submit'])) {
$dompdf = new Dompdf();
$tables = '';
for ($i = 0; $i < count($_POST['servproname']); $i++) {
$tables .= " <table>
<tr>
<td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['servproname'][$i]}</td>
<td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['docname'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['price1'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['qty'][$i]}</td>
<td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['tot4'][$i]}</td>
</tr>
</table>";
}
$dompdf->loadHtml("<html> $tables </html>");
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("", array("Attachment" => false));
exit(0);
}
As you can see i made a variable called $tables which is a string of what you where trying to make.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论