【问题标题】:Creating html table php创建html表php
【发布时间】:2016-09-16 00:52:30
【问题描述】:

大家好,我是 php 新手。我想创建一个 html 表的字符串,以便以后使用它 mpdf,但是在构建我的表的过程中,代码似乎还可以,但是没有生成数据的 html 表。

下面是我的代码:

 $html ='<div class="row">
                <div class="col-lg-12">
                 <table  width="100%" class="table table-striped table bordered">
                    <thead>
                    <tr>
                    <th class="text-left">Code</th>
                    <th class="text-left">Phone Number</th>
                    <th class="text-left">Delivery Date</th>
                    <th class="text-left">Amount HT</th>
                    <th class="text-left">Amount TVA</th>
                    <th class="text-left">Amount TTC</th>
                    <th class="text-left">Payment Mode</th>
                </tr>
                </thead >
                <tbody>';
                    foreach($estimateArray as $item):
                        if(($startDate <= date_create($item->created) && date_create($item->created) <= $endDate)){
                            $html .=  ' <tr>
                            <td class="text-left">'; echo $item->customer_name;'</td>
                            <td class="text-left">'; echo $item->document_number;'</td>
                            <td class="text-left">'; echo $item->delivery_date;'</td>
                            <td class="text-left">'; echo $item->amount_ht;'</td>
                            <td class="text-left">'; echo $item->amount_tva;'</td>
                            <td class="text-left">'; echo $item->amount_ttc;'</td>
                            <td class="text-left">'; echo $item->payment_mode;'</td>
                        </tr>';

                        } endforeach;
                        $html .= '
                                    </tbody>
                                </table>
                            </div>
                     </div>
        ';

我需要帮助。

【问题讨论】:

    标签: php html mpdf


    【解决方案1】:

    您正在使用 echo 而不是将属性值附加到 $html 变量。

    尝试类似:

    <td class="text-left">' . $item->customer_name . '</td>
    

    编辑:以这种方式在字符串中使用变量更清晰:

    $output = "<td class=\"text-left\">{$item->customer_name}</td>";
    

    【讨论】:

      【解决方案2】:

      将值附加到您的 $html 而不是回显。

      foreach($estimateArray as $item):
      if(($startDate <= date_create($item->created) && date_create($item->created) <= $endDate)){
          $html .=  ' <tr>
          <td class="text-left">'. $item->customer_name . '</td>
          <td class="text-left">'. $item->document_number . '</td>
          <td class="text-left">'. $item->delivery_date.'</td>
          <td class="text-left">'. $item->amount_ht.'</td>
          <td class="text-left">'. $item->amount_tva.'</td>
          <td class="text-left">'. $item->amount_ttc.'</td>
          <td class="text-left">'. $item->payment_mode.'</td>
      </tr>';
      
      } endforeach;
      

      【讨论】:

        【解决方案3】:

        为了澄清这一点,请尝试添加此代码。

                     foreach($estimateArray as $item):
                            if(($startDate <= date_create($item->created) && date_create($item->created) <= $endDate)){
                                $html .=  ' <tr>
                                <td class="text-left">'.$item->customer_name.'</td>
                                <td class="text-left">'.$item->document_number.'</td>
                                <td class="text-left">'.$item->delivery_date.'</td>
                                <td class="text-left">'.$item->amount_ht.'</td>
                                <td class="text-left">'.$item->amount_tva.'</td>
                                <td class="text-left">'.$item->amount_ttc.'</td>
                                <td class="text-left">'.$item->payment_mode.'</td>
                            </tr>';
        
                            } endforeach;
        

        【讨论】:

          【解决方案4】:

          我可以推荐三种方法。

          1。方法

          关闭 PHP 代码并在 HTML 中添加 PHP 代码。

          <?php
          // there is PHP codes
          ?>
          <table>
            <tr>
              <td><?php echo $value; ?></td>
            </tr>
          </table>
          <?php
          // continue with PHP
          ?>
          

          2。方法

          用 PHP 打印 HTML 代码。

          记住!要使用 PHP 值,请使用双引号 (") 打印。如果您需要使用双引号,请使用转义字符 (\)。 (例如:class=\"demoTable\"

          <?php
          echo "
            <table class='demoTable'>
              <tr>
                <td>$value</td>
              </tr>
            </table>
          ";
          ?>
          

          3。方法

          使用 PHP heredocnowdoc

          现在您可以在代码中使用双引号和单引号。

          <?php
          $html = <<<HTML
            <table class="demoTable" style="font-family: 'Open Sans';">
              <tr>
                <td>$value</td>
              </tr>
            </table>
          HTML;
          echo $html;
          ?>
          

          【讨论】:

            猜你喜欢
            • 2014-07-17
            • 2011-05-20
            • 2010-10-17
            • 2017-10-05
            • 1970-01-01
            • 1970-01-01
            • 2012-11-23
            • 2012-07-01
            相关资源
            最近更新 更多