1

Hi I have a problem in sending data from php to pdf. I think the only way is to put in a variable the data that will be send to pdf function. here is the code that i need to put in a php variable..

<table width="95%" height="95%">
            <tr>
                <td align="center">
                  <table width="100%" border="0" cellpadding="2" cellspacing="2">
                    <tr align="center">
                      <td colspan="7" bgcolor="#2561cf" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
                    </tr>
                    <tr>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Sun</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Mon</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Tue</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Wed</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Thu</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Fri</strong></td>
                      <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Sat</strong></td>
                    </tr>

                    <?php 
                        $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                        $maxday    = date("t",$timestamp);
                        $thismonth = getdate ($timestamp);
                        $startday  = $thismonth['wday'];

                      for ($i=0; $i<($maxday+$startday); $i++) {
                        $day = ($i - $startday + 1);
                        if($day['1'] == '1' || $day['1'] == '2' || $day['1'] == '3' || $day['1'] == '4' || $day['1'] == '5' || $day['1'] == '6' || $day['1'] == '7' || $day['1'] == '8' || $day['1'] == '9'){
                            $day    = '0'.$day;
                        }else{
                            $day    = $day;
                        }
                        $date = $monthNames[$cMonth-1].' '.$day.', '.$cYear;
                        $date2 = $cMonth.'/'.$day.'/'.$cYear;
                        $events = $this->getEvents($date2);
                        if(($i % 7) == 0 ) echo "<tr>\n";
                        if($i < $startday) echo "<td></td>\n";
                        else echo "<td valign='top' align='left' width='50px' height='50px'><div class='".$dateId."' id='".$date."' title='".$date2."' style='cursor:pointer;'>". ($i - $startday + 1) .'</div>'. $events."</td>\n";
                        if(($i % 7) == 6 ) echo "</tr>\n";
                      }  
                     ?>
                  </table>
                </td>
            </tr>
         </table>
3
  • Can you explain the question a bit more? Commented Mar 16, 2012 at 5:56
  • I see a lot of redundant code here. Commented Mar 16, 2012 at 5:57
  • There is no need for else{ $day = $day; } Commented Mar 16, 2012 at 5:57

4 Answers 4

4

If you have a huge piece of HTML/PHP and you need the output stored in a variable instead of printed to the screen, you can use output buffering functions to make this easy:

<?php
ob_start(); // Start the buffer (nothing will be output)
?>
<p>Lots and lots of mixed <?php echo 'HTML'; ?> and PHP...</p>
<?php
$content = ob_get_clean(); // End the buffer and assign everything to $content
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It does no show the warning anymore but the pdf output is empty.
Sounds like you have a separate problem deserving of a new question. If you do decide to ask, make sure to include your code and what PDF library you're using. I also suggest using only a small amount of sample data until you get it to work, and check the documentation of the tool you're using.
1

It is possible just try concatenate the loop of just put your code inside a function. Ex.

function test(){
    echo '{the code}';

}

$var = test(); 

Comments

0

Although I don't fully understand what you're asking I will try to answer it!

If you want everything that you are currently displaying to the page using PHP in a variable, instead of using echo, you are going to want to append that information onto your variable, so...

                <?php 
                    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                    $maxday    = date("t",$timestamp);
                    $thismonth = getdate ($timestamp);
                    $startday  = $thismonth['wday'];

                  for ($i=0; $i<($maxday+$startday); $i++) {
                    $day = ($i - $startday + 1);
                    if($day['1'] == '1' || $day['1'] == '2' || $day['1'] == '3' || $day['1'] == '4' || $day['1'] == '5' || $day['1'] == '6' || $day['1'] == '7' || $day['1'] == '8' || $day['1'] == '9'){
                        $day    = '0'.$day;
                    }else{
                        $day    = $day;
                    }
                    $date = $monthNames[$cMonth-1].' '.$day.', '.$cYear;
                    $date2 = $cMonth.'/'.$day.'/'.$cYear;
                    $events = $this->getEvents($date2);
                    if(($i % 7) == 0 ) echo "<tr>\n";
                    if($i < $startday) echo "<td></td>\n";
                    else echo "<td valign='top' align='left' width='50px' height='50px'><div class='".$dateId."' id='".$date."' title='".$date2."' style='cursor:pointer;'>". ($i - $startday + 1) .'</div>'. $events."</td>\n";
                    if(($i % 7) == 6 ) echo "</tr>\n";
                  }  
                 ?>

Would become...

                <?php 
                    $send_me_to_pdf = '';
                    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                    $maxday    = date("t",$timestamp);
                    $thismonth = getdate ($timestamp);
                    $startday  = $thismonth['wday'];

                  for ($i=0; $i<($maxday+$startday); $i++) {
                    $day = ($i - $startday + 1);
                    if($day['1'] == '1' || $day['1'] == '2' || $day['1'] == '3' || $day['1'] == '4' || $day['1'] == '5' || $day['1'] == '6' || $day['1'] == '7' || $day['1'] == '8' || $day['1'] == '9'){
                        $day    = '0'.$day;
                    }else{
                        $day    = $day;
                    }
                    $date = $monthNames[$cMonth-1].' '.$day.', '.$cYear;
                    $date2 = $cMonth.'/'.$day.'/'.$cYear;
                    $events = $this->getEvents($date2);
                    if(($i % 7) == 0 ) $send_me_to_pdf .= "<tr>\n";
                    if($i < $startday) $send_me_to_pdf .= "<td></td>\n";
                    else $send_me_to_pdf .= "<td valign='top' align='left' width='50px' height='50px'><div class='".$dateId."' id='".$date."' title='".$date2."' style='cursor:pointer;'>". ($i - $startday + 1) .'</div>'. $events."</td>\n";
                    if(($i % 7) == 6 ) $send_me_to_pdf .= "</tr>\n";
                  }  
                 ?>

Now, you can use $send_me_to_pdf as an argument to your PDF functions where you want to send this data?

Comments

0

Try concating variable

    <?php
    $var='
    <table width="95%" height="95%">
                <tr>
                    <td align="center">
                      <table width="100%" border="0" cellpadding="2" cellspacing="2">
                        <tr align="center">
                          <td colspan="7" bgcolor="#2561cf" style="color:#FFFFFF"><strong>'.$monthNames[$cMonth-1].' '.$cYear.'</strong></td>
                        </tr>
                        <tr>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Sun</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Mon</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Tue</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Wed</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Thu</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Fri</strong></td>
                          <td align="center" bgcolor="#2561cf" style="color:#FFFFFF"><strong>Sat</strong></td>
                        </tr>';
    ?>
                        <?php 
                            $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                            $maxday    = date("t",$timestamp);
                            $thismonth = getdate ($timestamp);
                            $startday  = $thismonth['wday'];

                          for ($i=0; $i<($maxday+$startday); $i++) {
                            $day = ($i - $startday + 1);
                            if($day['1'] == '1' || $day['1'] == '2' || $day['1'] == '3' || $day['1'] == '4' || $day['1'] == '5' || $day['1'] == '6' || $day['1'] == '7' || $day['1'] == '8' || $day['1'] == '9'){
                                $day    = '0'.$day;
                            }else{
                                $day    = $day;
                            }
                            $date = $monthNames[$cMonth-1].' '.$day.', '.$cYear;
                            $date2 = $cMonth.'/'.$day.'/'.$cYear;
                            $events = $this->getEvents($date2);
                            if(($i % 7) == 0 ) echo "<tr>\n";
                            if($i < $startday) echo "<td></td>\n";
                            $var.= "<td valign='top' align='left' width='50px' height='50px'><div class='".$dateId."' id='".$date."' title='".$date2."' style='cursor:pointer;'>". ($i - $startday + 1) .'</div>'. $events."</td>\n";

if(($i % 7) == 6 ) 
$var.="</tr>\n";
                          }  

                     $var.=' </table>
                    </td>
                </tr>
             </table>';

    ?>

Final result is in $var

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.