Creating html table php

2019-03-06 11:55发布

Hi guys I'm quite new in php. I want to create a string of html table to later use it mpdf but in the course of constructing my table the code seems ok but no html table with data generated just the header.

Below is my code:

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

Please I need help.

标签: php html mpdf
4条回答
别忘想泡老子
2楼-- · 2019-03-06 11:58

You are using echo instead of appending property value to $html variable.

Try something like:

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

Edit: It's much more clearer using variables in string this way:

$output = "<td class=\"text-left\">{$item->customer_name}</td>";
查看更多
干净又极端
3楼-- · 2019-03-06 12:05

Append the values to your $html instead of echoing.

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楼-- · 2019-03-06 12:14

Just to clarify this, try dropping in this code.

             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;
查看更多
可以哭但决不认输i
5楼-- · 2019-03-06 12:19

There are three methods I can recommend.

1. method

Close PHP code and add PHP code in HTML.

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

2. method

Print HTML code with PHP.

Remember! To work with PHP values, print with double quotes ("). Use escape character (\) if you need to use double quotes. (Ex: class=\"demoTable\")

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

3. method

Use PHP heredoc or nowdoc.

Now you can use double and single quotes in your code.

<?php
$html = <<<HTML
  <table class="demoTable" style="font-family: 'Open Sans';">
    <tr>
      <td>$value</td>
    </tr>
  </table>
HTML;
echo $html;
?>
查看更多
登录 后发表回答