I am trying to use tcpdf library to generate 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. 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
我正在尝试使用tcpdf库生成pdf。我有一个php文件,其中包含名称,公司名称等变量。但是为了显示产品,我将数组传递给php。现在我想在表格行中显示数组的每个元素,因为我必须编写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...';
4 个解决方案
#1
0
You can't continue the html with a concatenation straight after the foreach. You have to do $html .=
again.
你不能在foreach之后直接连接html。你必须再做$ 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>';
#2
0
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...';
#3
0
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...';
#4
0
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 ? 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.
请注意,即使这里HTML代码仍然正确突出显示?它可能与您选择的编辑器中的相同。我发现它更具可读性,甚至可以避免可能的字符转义问题。
NOTE : I didn't fix your HTML, but there are several bad practices in it regarding semantics or even use of obsolete/deprecated tags. Here are a few :
注意:我没有修复你的HTML,但是它中有一些关于语义甚至使用过时/弃用标记的不良做法。以下是一些:
- Don't use anything changing layout in your HTML, this is what CSS are for. For example, don't use
border
orcellpadding
attributes on your table, usetable {border: 1px solid #ccc;}
(you can of course change color, that's an example) andtable td {padding: 5px;}
instead. - 不要在HTML中使用任何改变布局的东西,这就是CSS的用途。例如,不要在表上使用border或cellpadding属性,使用表{border:1px solid #ccc;}(当然可以更改颜色,这是一个示例)和table td {padding:5px;}。
- Semantic best practices also imply not using
<br>
tag. Better define top or bottom margins to put some space between elements. (I must admit that's probably the only example where I value ease over semantic and sometimes use it myself though) -
语义最佳实践也意味着不使用
标签。更好地定义顶部或底部边距以在元素之间放置一些空间。 (我必须承认,这可能是我重视语义的唯一例子,有时我自己也会使用它) -
<b>
tag should be use only in very specific cases, cfr this article. In this specific case you can achiever the same effect (bold text) by usingfont-weight: bold
on the desired cells, either by giving them a specific CSS class, either by targetting them with a CSS3 selector like for example :tr td:nth-child(3) {font-weight: bold;}
} - 标签应仅在非常具体的情况下使用,请参阅本文。在这种特定情况下,你可以通过在所需的单元格上使用font-weight:bold来实现相同的效果(粗体文本),或者通过给它们一个特定的CSS类,或者通过使用CSS3选择器来定位它们,例如:tr td: nth-child(3){font-weight:bold;}}
- similarily, don't use
align
attribute, same thing, target the desired cell and use CSS propertytext-align: right;
instead. - 同样地,不要使用align属性,同样的事情,定位所需的单元格并使用CSS属性text-align:right;代替。
#1
0
You can't continue the html with a concatenation straight after the foreach. You have to do $html .=
again.
你不能在foreach之后直接连接html。你必须再做$ 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>';
#2
0
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...';
#3
0
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...';
#4
0
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 ? 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.
请注意,即使这里HTML代码仍然正确突出显示?它可能与您选择的编辑器中的相同。我发现它更具可读性,甚至可以避免可能的字符转义问题。
NOTE : I didn't fix your HTML, but there are several bad practices in it regarding semantics or even use of obsolete/deprecated tags. Here are a few :
注意:我没有修复你的HTML,但是它中有一些关于语义甚至使用过时/弃用标记的不良做法。以下是一些:
- Don't use anything changing layout in your HTML, this is what CSS are for. For example, don't use
border
orcellpadding
attributes on your table, usetable {border: 1px solid #ccc;}
(you can of course change color, that's an example) andtable td {padding: 5px;}
instead. - 不要在HTML中使用任何改变布局的东西,这就是CSS的用途。例如,不要在表上使用border或cellpadding属性,使用表{border:1px solid #ccc;}(当然可以更改颜色,这是一个示例)和table td {padding:5px;}。
- Semantic best practices also imply not using
<br>
tag. Better define top or bottom margins to put some space between elements. (I must admit that's probably the only example where I value ease over semantic and sometimes use it myself though) -
语义最佳实践也意味着不使用
标签。更好地定义顶部或底部边距以在元素之间放置一些空间。 (我必须承认,这可能是我重视语义的唯一例子,有时我自己也会使用它) -
<b>
tag should be use only in very specific cases, cfr this article. In this specific case you can achiever the same effect (bold text) by usingfont-weight: bold
on the desired cells, either by giving them a specific CSS class, either by targetting them with a CSS3 selector like for example :tr td:nth-child(3) {font-weight: bold;}
} - 标签应仅在非常具体的情况下使用,请参阅本文。在这种特定情况下,你可以通过在所需的单元格上使用font-weight:bold来实现相同的效果(粗体文本),或者通过给它们一个特定的CSS类,或者通过使用CSS3选择器来定位它们,例如:tr td: nth-child(3){font-weight:bold;}}
- similarily, don't use
align
attribute, same thing, target the desired cell and use CSS propertytext-align: right;
instead. - 同样地,不要使用align属性,同样的事情,定位所需的单元格并使用CSS属性text-align:right;代替。