将 HTML 代码与 PHP 代码混合在一起


mixing html code with php code

我正在尝试使用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...';
与其将

你的 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...

请注意,即使在这里,HTML代码仍然正确突出显示?在您选择的编辑器中可能相同。我发现它的可读性更强,您甚至可以避免可能的字符转义问题。

注意:我没有修复您的 HTML,但其中有一些关于语义甚至使用过时/已弃用标签的不良做法。以下是一些:

  • 不要在HTML中使用任何更改布局的东西,这就是CSS的用途。例如,不要在表上使用bordercellpadding属性,而是使用table {border: 1px solid #ccc;}(您当然可以更改颜色,这是一个示例)和table td {padding: 5px;}
  • 语义最佳实践还意味着不使用<br>标记。最好定义上边距或下边距,以便在元素之间留出一些空间。(我必须承认,这可能是我重视易用性而不是语义的唯一例子,有时我自己也使用它)
  • <b>标签应仅在非常特殊的情况下使用,参见本文。在这种特定情况下,您可以通过在所需单元格上使用font-weight: bold来实现相同的效果(粗体文本),或者通过为它们提供特定的 CSS 类,或者通过使用 CSS 选择器来定位它们,例如:tr td:nth-child(3) {font-weight: bold;} }
  • 同样,不要使用align属性,相同的内容,定位所需的单元格并使用CSS属性text-align: right;
你不能在

foreach之后直接用串联来继续html。你必须再做一次$html .=

</tr>'.
foreach($item as products): .'   //This part contains error
<tr>

应该是这个

</tr>';
foreach($item as products): $html .= '   //This part contains error
<tr>

foreach的末尾也是如此:

</tr>';
endforeach;
$html .= '</table>';

你做错了。这应该有效 -

$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...';

这样的事情的正确方法

 $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...';