将 html 代码与 php 代码混合

mixing html code with php code

我正在尝试使用 tcpdf 库生成 pdf。我有一个 php 文件,其中包含名称、公司名称等变量。但是为了显示产品,我将数组传递给 php。现在我想在 table 行中显示数组的每个元素,为此我必须编写 php 代码,但不知何故我在将 PHP 代码与 html 混合时遇到问题 code.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): .'   //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...';

您不能在 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...';

我不会将你的 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,但其中有一些关于语义甚至使用 obsolete/deprecated 标签的不良做法。这里有一些:

  • 不要在 HTML 中使用任何改变布局的东西,这就是 CSS 的用途。例如,不要在 table 上使用 bordercellpadding 属性,请使用 table {border: 1px solid #ccc;}(您当然可以更改颜色,这是一个示例)和 table td {padding: 5px;} 相反。
  • 语义最佳实践也意味着不使用 <br> 标签。最好定义顶部或底部边距以在元素之间放置一些 space。 (我必须承认,这可能是唯一一个我重视易用性而不是语义的例子,尽管有时我自己也会使用它)
  • <b> 标签应该只在非常特殊的情况下使用,cfr this article。在这种特定情况下,您可以通过在所需单元格上使用 font-weight: bold 来实现相同的效果(粗体文本),或者通过给它们特定的 CSS class,或者通过使用 CSS 选择器,例如:tr td:nth-child(3) {font-weight: bold;}}
  • 类似地,不要使用 align 属性,同样,定位所需的单元格并使用 CSS 属性 text-align: right; 代替。