2011-12-29 102 views
0

我正在使用DOMpdf創建PDF文件。要打印下面的表格,我必須把它作爲一個變量,然後發送給我的控制器。就像$value = "Some value(in this place I want the following table) ";但是在這種情況下,我不確定當你有一些PHP腳本來填充數據時,如何將這個整個表格放入變量中。如何獲得一個HTML表格作爲PHP變量?

請幫助。

<?php if(count($records) > 0) { ?> 
      <h1> Batch Name: <?php echo "$batchname";?> </h1> 
      <table id="table1" class="gtable sortable"> 
      <thead> 
        <tr> 
         <th>S.N</th> 
         <th>Student ID</th> 
         <th>Exam Date</th> 
         <th>Exam Type</th> 
         <th>Subject</th> 
         <th>Total Mark</th> 
         <th>Highest Mark</th> 
         <th>Obtained Mark</th> 
         <th>GPA</th> 
         <th>Grade</th> 
         <th>Status</th> 

        </tr> 
      </thead> 
      <tbody> 
      <?php $i = $this->uri->segment(3) + 0; foreach ($records as $row){ $i++; ?> 


        <tr> 
         <td><?php echo $i; ?>.</td> 

         <td><a href="<?php echo base_url(); ?>viewbatch/get/<?php echo $row['studentid']; ?>"><?php echo $row['studentid'];?></a></td> 
         <td><?php echo $row['examdate'];?></td> 
         <td><?php echo $row['examtype'];?></td> 

         <td><?php echo $row['subject'];?></td> 
         <td><?php echo $row['totalmark'];?></td> 

         <td><?php echo $row['highestmark'];?></td> 
         <td><?php echo $row['obtainedmark'];?></td> 

         <td><?php echo $row['gradepoint'];?></td> 
         <td><?php echo $row['grade'];?></td> 
         <td><?php echo $row['status'];?></td> 



        </tr> 
      <?php } ?> 

      </tbody> 
      </table> 
+1

看看我的回覆:http://stackoverflow.com/questions/8657831/how-to-after-pulling-info-from-db-send-an-email/8657946#8657946,你必須告訴PHP緩衝數據而不是發送給瀏覽器,通常繪製HTML,然後將緩衝的內容從PHP返回爲字符串,此時您將擁有包含剛繪製的HTML的字符串。 – Yaniro 2011-12-29 07:54:59

回答

2

試試這個代碼。

<?php 
ob_start(); 
if(count($records) > 0) { ?> 
      <h1> Batch Name: <?php echo "$batchname";?> </h1> 
      <table id="table1" class="gtable sortable"> 
      <thead> 
        <tr> 
         <th>S.N</th> 
         <th>Student ID</th> 
         <th>Exam Date</th> 
         <th>Exam Type</th> 
         <th>Subject</th> 
         <th>Total Mark</th> 
         <th>Highest Mark</th> 
         <th>Obtained Mark</th> 
         <th>GPA</th> 
         <th>Grade</th> 
         <th>Status</th> 

        </tr> 
      </thead> 
      <tbody> 
      <?php $i = $this->uri->segment(3) + 0; foreach ($records as $row){ $i++; ?> 


        <tr> 
         <td><?php echo $i; ?>.</td> 

         <td><a href="<?php echo base_url(); ?>viewbatch/get/<?php echo $row['studentid']; ?>"><?php echo $row['studentid'];?></a></td> 
         <td><?php echo $row['examdate'];?></td> 
         <td><?php echo $row['examtype'];?></td> 

         <td><?php echo $row['subject'];?></td> 
         <td><?php echo $row['totalmark'];?></td> 

         <td><?php echo $row['highestmark'];?></td> 
         <td><?php echo $row['obtainedmark'];?></td> 

         <td><?php echo $row['gradepoint'];?></td> 
         <td><?php echo $row['grade'];?></td> 
         <td><?php echo $row['status'];?></td> 



        </tr> 
      <?php } ?> 

      </tbody> 
      </table> 
<?php 
$output = ob_get_clean(); 
?> 

乾杯!