seco
seco

Reputation: 33

store part of the page in variable

i have a page contains a table and this table contains a lot of coding like this

<table class="table">
          <tbody>
          <tr >
            <td width="20" class="tabletop">م</td>
            <td class="tabletop" >name</td>
            <td class="tabletop" style="width:120px">date</td>
            <td class="tabletop" style="width:120px">note1</td>

             <td class="tabletop" style="width:100px">note2</td>
             <td class="tabletop" style="width:90px">note3</td>

          </tr>
          <? $res=mysql_query($sql);
                 while($resArr=mysql_fetch_array($res)){ ?>

          <tr style="width:700px">
            <td  class="tabletext"><?= ++$serial;?></td>
            <td class="tabletext" ><?= $resArr[stName];?></td>
             <td class="tabletext"><?= $resArr['date'];?></td>
            <td class="tabletext" ><?= $resArr[matName];?></td>

            <td class="tabletext" ><? if($resArr[exam]==1) echo "work";else echo "final";?></td>
             <td class="tabletext" ><? if($resArr[exam_type]==1) echo "prac";else echo "test";?></td>

          </tr>

          <? }?>
          </tbody>
       </table>

as you see the table has php coding now i want to store the whole table in variable so i can send it to pdf printing library tcpdf

Upvotes: 0

Views: 73

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 79004

Another way, if you already have all of this code and want to save it:

Before the table:

<?php ob_start(); ?>

After the table:

<?php $output = ob_get_contents(); ?>

The table will still be displayed and you can use $output to send to the PDF.

Upvotes: 0

hopsoo
hopsoo

Reputation: 305

You can use heredoc syntax, but you need to move the conditionals outside:

<?php

if($resArr['exam']==1) $exam = "work"; else $exam = "final";
if($resArr["exam_type"]==1) $examtype = "prac";else $examtype = "test";

$var = <<<EOT
<table class="table">
      <tbody>
      <tr >
        <td width="20" class="tabletop">م</td>
        <td class="tabletop" >name</td>
        <td class="tabletop" style="width:120px">date</td>
        <td class="tabletop" style="width:120px">note1</td>

         <td class="tabletop" style="width:100px">note2</td>
         <td class="tabletop" style="width:90px">note3</td>

      </tr>
      $res=mysql_query($sql);
             while($resArr=mysql_fetch_array($res)){

      <tr style="width:700px">
        <td  class="tabletext">{++$serial}</td>
        <td class="tabletext" >{$resArr["stName"]}</td>
         <td class="tabletext">{$resArr["date"]}</td>
        <td class="tabletext" >{$resArr["matName"]}</td>

        <td class="tabletext" >{$exam}</td>
         <td class="tabletext" >{$examtype}</td>

      </tr>

      <? }?>
      </tbody>
   </table>        
EOT;

echo $var;

Upvotes: 1

Related Questions