循环数组 PHP 不能在 pdf tcpdf 上工作

Looping array PHP can't work on pdf tcpdf

我要显示 3 年 2012, 2013,2014

但现在只输出 2014 年,无法循环.. 你知道如何像这样在 tcpdf 上循环数组吗?

    <?php 

       require_once ("tcpdfs/tcpdf.php");
       $yp=array('2012','2013','2014');

       $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

       $pdf->SetFont('times', 'A4', 11);
       $pdf->addPage();

       //eror here
       $htmlab = '<table width="710" border="1" cellspacing="0" cellpadding="2">
                  <tr>';

                  for($i=0;$i<count($yp);$i++){
                   $htmlab = '<th style="text-align:center;" width="90">'.$yp[$i].'</th>';
                  }
       $htmlac = '</tr>
                  </table>';

       $pdf->writeHTML($htmlaa, false, false, true, false, '');
       $pdf->writeHTML($htmlab, false, false, true, false, '');
       $pdf->writeHTML($htmlac, false, false, true, false, '');

       $html = ob_get_contents();
       ob_end_clean();   
       $pdf->Output('years.pdf', 'I');


    ?>

这是因为您的 $htmllab 变量被每次循环执行覆盖:

这样做

 for($i=0;$i<count($yp);$i++) {
    $htmlab.= '<th style="text-align:center;" width="90">'.$yp[$i].'</th>';
 }

HTML代码

<form class="contact-us form-horizontal" action="pdf/examples/pdf.php" method="post" enctype="multipart/form-data">
    <legend>Fill Form and submit to generate PDF</legend>  
    <br>     
    <div class="control-group">
        <label class="control-label">Name</label>
        <div class="controls">
            <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
                <input type="text" class="input-xlarge" name="name" placeholder="Name">
            </div>
        </div>
    </div>
    <br>
    <div class="control-group">
        <label class="control-label">Email</label>
        <div class="controls">
            <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
                <input type="email" class="input-xlarge" name="email" placeholder="Name">
            </div>
        </div>
    </div>
    <br>
    <div class="control-group">
        <label class="control-label">Address</label>
        <div class="controls">
            <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
                <textarea name="address"></textarea>
            </div>
        </div>
    </div>
    <br>
    <div class="control-group">
        <label class="control-label">Languages</label>
        <div class="controls">
            <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
                <input type="checkbox" name="l[]" value="English" />English<br />
                <input type="checkbox" name="l[]" value="Hindi" />Hindi<br />
                <input type="checkbox" name="l[]" value="Tamil" />Tamil<br />
            </div>
        </div>
    </div>
    <br>
    <div class="control-group">
        <label class="control-label">Images</label>
        <div class="controls">
            <div class="input-prepend">
            <span class="add-on"><i class="icon-envelope"></i></span>
                <input type="file" class="input-xlarge" name="image" placeholder="image">
            </div>
        </div>    
    </div>
    <hr>
    <div class="control-group">
      <div class="controls">
        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
        <button type="button" class="btn">Cancel</button>
      </div>    
    </div>
  </form>

PHP代码

    if(isset($_POST['submit'])){

        $name = $_POST['name'];
        $email = $_POST['email'];
        $address = $_POST['address'];
        $lang = $_POST['l'];
        $date = Date('M d, Y');

        $file_tmp_name = $_FILES['image']['tmp_name'];
        $str = file_get_contents($file_tmp_name);
        $b64img=base64_encode($str);

        $_SESSION['image'] = $b64img; // holds your image string in session without saving it.

        $N = count($lang);      

        $html = '';
        // create some HTML content
        $html .= '

        <table style="width:100%">
          <tr>
            <th>Name</th>
            <th>Email</th> 
            <th>Address</th>
            <th>Languages</th>
            <th>Profile</th>
          </tr>
          <tr>
            <td>'.$name.'</td>
            <td>'.$email.'</td> 
            <td>'.$address.'</td>
            <td>';

        for($i=0; $i < $N; $i++) {
            $html.= $lang[$i];
         }

        $html .='</td>
            <td><img src="data:image/jpg;base64,'.$_SESSION['image'].'" width="200"></td>
          </tr>
        </table>

        ';


        // output the HTML content
        $pdf->writeHTML($html, true, false, true, false, '');