如何在php中回显循环并将其放入变量中

how to echo loop in php and put it in a variable?

本文关键字:变量 php 循环      更新时间:2023-09-26

嗨,我在将数据从php发送到pdf时遇到问题。我认为唯一的方法是在一个变量中放入将发送到pdf函数的数据。以下是我需要放入php变量中的代码。。

<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>

如果你有一大块HTML/PHP,并且你需要将输出存储在一个变量中,而不是打印到屏幕上,你可以使用输出缓冲函数来简化这一点:

<?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
?>

只需尝试将代码放入函数中的循环连接即可。示例

function test(){
    echo '{the code}';
}
$var = test(); 

虽然我不完全理解你在问什么,但我会尽力回答的!

如果你想在一个变量中使用PHP将当前显示在页面上的所有信息,而不是使用echo,你会想将这些信息附加到你的变量上,所以…

                <?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";
                  }  
                 ?>

会变成。。。

                <?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";
                  }  
                 ?>

现在,您可以使用$send_me_to_pdf作为pdf函数的参数,将这些数据发送到哪里?

尝试连接变量

    <?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>';
    ?>

最终结果在$var