[英]mixing html code with php code
I am trying to use tcpdf library to generate pdf.我正在尝试使用 tcpdf 库来生成 pdf。 I have a php file which contains variables like name,company name etc. But for displaying the products I am passing the array to php.
我有一个 php 文件,其中包含名称、公司名称等变量。但是为了显示产品,我将数组传递给 php。 Now I want to display each element of array in a table row for that I have to write the php code but somehow I am having problem mixing PHP code with html code.Here is the part whihch contains the problem
现在我想在表行中显示数组的每个元素,因为我必须编写 php 代码,但不知何故我在将 PHP 代码与 html 代码混合时遇到了问题。这是包含问题的部分
$html .= '<br><br>
<table border="1" cellpadding="5">
<tr>
<td colspan="3">Invoice # {invoice_ref_id}</td>
</tr>
<tr>
<td><b>Product</b></td>
<td><b>Quantity</b></td>
<td align="right"><b>Amount (Rs.)</b></td>
</tr>'.
foreach($item as products): .' //This part contains error
<tr>
<td>'echo products['item']'</td>
<td>'echo products['quantity']'</td>
<td align="right">75</td>
</tr>
<tr>
<td colspan="3" align="right"><b>Total: 375</b></td>
</tr>'
endforeach;
'</table>';
$html .= '<br><br>Some more text can come here...';
You can't continue the html with a concatenation straight after the foreach.您不能在 foreach 之后直接通过串联继续 html。 You have to do
$html .=
again.您必须再次执行
$html .=
。
This这个
</tr>'.
foreach($item as products): .' //This part contains error
<tr>
should be this应该是这个
</tr>';
foreach($item as products): $html .= ' //This part contains error
<tr>
The same goes at the end of the foreach:在 foreach 结束时也是如此:
</tr>';
endforeach;
$html .= '</table>';
You are doing it wrong way.你做错了。 This should work -
这应该有效 -
$html .= '<br><br>
<table border="1" cellpadding="5">
<tr>
<td colspan="3">Invoice # {invoice_ref_id}</td>
</tr>
<tr>
<td><b>Product</b></td>
<td><b>Quantity</b></td>
<td align="right"><b>Amount (Rs.)</b></td>
</tr>';
foreach($item as products) { //This part contains error
$html .= '<tr>
<td>'. products['item']. '</td>
<td>'. products['quantity']. '</td>
<td align="right">75</td>
........ ';
}
$html .= '</table>';
$html .= '<br><br>Some more text can come here...';
thie right way of doing something like this做这样的事情的正确方法
$html .= '<br><br>
<table border="1" cellpadding="5">
<tr>
<td colspan="3">Invoice # {invoice_ref_id}</td>
</tr>
<tr>
<td><b>Product</b></td>
<td><b>Quantity</b></td>
<td align="right"><b>Amount (Rs.)</b></td>
</tr>';
foreach($item as products):
$html .=' //This part contains error
<tr>
<td>'echo products['item']'</td>
<td>'echo products['quantity']'</td>
<td align="right">75</td>
</tr>
<tr>
<td colspan="3" align="right"><b>Total: 375</b></td>
</tr>';
endforeach;
$html .= '</table>';
$html .= '<br><br>Some more text can come here...';
Instead of concatenating your HTML in a string that you will then need to echo, I would go to something like this :而不是将您的 HTML 连接到一个您需要回显的字符串中,我会去这样的:
<br><br>
<table border="1" cellpadding="5">
<tr>
<td colspan="3">Invoice # {invoice_ref_id}</td>
</tr>
<tr>
<td><b>Product</b></td>
<td><b>Quantity</b></td>
<td align="right"><b>Amount (Rs.)</b></td>
</tr>
<?php foreach($item as products){ ?>
<tr>
<td><?php echo products['item'] ?></td>
<td><?php echo products['quantity'] ?></td>
<td align="right">75</td>
</tr>
<tr>
<td colspan="3" align="right"><b>Total: 375</b></td>
</tr>
<?php> } //ending the foreach ?>
</table>
<br><br>Some more text can come here...
Notice how even here HTML code is still correctly highlighted?请注意即使在这里 HTML 代码仍然正确突出显示? It will probably be the same in your editor of choice.
在您选择的编辑器中它可能是相同的。 I find it much more readable, and you even avoid possible character escaping issues.
我发现它更具可读性,您甚至可以避免可能的字符转义问题。
NOTE : I didn't fix your HTML, but there are several bad practices in it regarding semantics or even use of obsolete/deprecated tags.注意:我没有修复您的 HTML,但其中有一些关于语义甚至使用过时/弃用标签的不良做法。 Here are a few:
以下是一些:
border
or cellpadding
attributes on your table, use table {border: 1px solid #ccc;}
(you can of course change color, that's an example) and table td {padding: 5px;}
instead.border
或单元格cellpadding
属性,而是使用table {border: 1px solid #ccc;}
(您当然可以更改颜色,这是一个示例)和table td {padding: 5px;}
。<br>
tag.<br>
标签。 Better define top or bottom margins to put some space between elements.<b>
tag should be use only in very specific cases, cfr this article . <b>
标签应该只在非常特殊的情况下使用,参见这篇文章。 In this specific case you can achieve the same effect (bold text) by using font-weight: bold
on the desired cells, either by giving them a specific CSS class, either by targetting them with a CSS selector like for example : tr td:nth-child(3) {font-weight: bold;}
}font-weight: bold
来实现相同的效果(粗体文本),或者通过为它们提供特定的 CSS 类,或者通过使用 CSS 选择器来定位它们,例如: tr td:nth-child(3) {font-weight: bold;}
}align
attribute, same thing, target the desired cell and use CSS property text-align: right;
align
属性,同样的事情,定位所需的单元格并使用 CSS 属性text-align: right;
instead.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.