如何在php中回显循环并将其放入变量中
how to echo loop in php and put it in a variable?
嗨,我在将数据从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
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 如何在php变量中嵌入JQuery代码
- 在javascript函数中使用php变量
- 将地理编码结果转换为php变量以发布到mysql数据库
- JavaScript,PHP-用JavaScript将多个数据库记录发送到变量或表
- 如何将变量php传递给javascript
- 显示动态变量(php和javascript)的总数
- 传递变量PHP、Javascript location.reload和HTML输入
- 需要将变量 php 的值传递给 javascript,但我不能
- 将变量从 .html 传递到太大而无法容纳在 URL 中的变量.php
- 如何在函数中传递javascript中的变量php
- 在Jquery中插入变量PHP
- 选择正确的变量 php/javascript
- 将HTML存储在变量PHP中,然后回显它
- 将变量PHP Smarty传递给Javascript
- 传递HTML表单post数据到不同的文件/网页javascript变量(php)
- 问题与传递变量(php, sql, jquery拖拽)
- 设置PHP中的javascaript变量(PHP在javascript中)
- 将变量php问题化为代码jquery