dra87370 2015-10-01 10:08 采纳率: 100%
浏览 50

PHP TCPDF heredoc

I got this in my heredoc in my TCPDF. I basically want to create a dynamic pdf with the data of my database.

        $html = <<<EOD
        <table border="1">
            <thead>
                <tr>
                    <th>firstname</th>
                    <th>lastname<th>
                </tr>
            </thead>
            <tbody>
                <tr>
                   <th></th>
                   <th></th>
                </tr>
            </tbody>
        </table>
EOD;

I want to make this dynamic like this, to create the data from my database dynamically.

    <?php
    foreach($result_set as $result) {
    ?>
    <tr>
        <td>
            <?php echo $result['firstname']; ?>
        </td>
        <td>
            <?php echo $result['lastname']; ?>
        </td>
    </tr>
    <?php
    }
    ?>

I tried this so far but I cannot find a suitable solution:

        $html = <<<EOD
        <table border="1">
            <thead>
                <tr>
                    <th>Vorname</th>
                    <th>Nachname</th>
                    <th>Von</th>
                    <th>Bis</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <?php echo $result['firstname']; ?>
                    </td>
                    <td>
                        <?php echo $result['lastname']; ?>
                    </td>
                </tr>
            </tbody>
        </table>
EOD;

can anyone help please.

  • 写回答

2条回答 默认 最新

  • dongshanyan0322 2015-10-01 10:37
    关注

    Ok I somehow did it, but still if there is any better solution feel free to post:)

    Code:

        $loopHereFirstname = '';
        $loopHereLastname  = '';
        foreach($result_set_random_01 as $result_dish_usr_01) {
            $tr_start = '<tr>';
            $tr_end   = '</tr>';
            $td_start = '<td>';
            $td_end   = '</td>';
            $loopHereFirstname .= $result_dish_usr_01['firstname']."
    ";
            $loopHereLastname  .= $result_dish_usr_01['lastname']."
    ";
        }
    
        $html = <<<EOD
        <table border="1">
            <thead>
                <tr>
                    <th>firstname</th>
                    <th>lastname</th>
                </tr>
            </thead>
            <tbody>
                $tr_start
                    $td_start
                        $loopHereFirstname
                    $td_end
                    $td_start
                        $loopHereLastname
                    $td_end
                $tr_end
            </tbody>
        </table>
    

    EOD;

    评论

报告相同问题?