我必须将帖子的内容传递给方法,才能正确地发布到Wordpress中。发布的所有内容都将进入传递给该方法的$ content变量。如何将foreach循环的内容放入$ content变量?
<?php
$return = '';
foreach ($response['daily']['data'] as $cond) {
echo '<br /><br /><strong>' . date('l, F j, Y', $cond['time']) .'</strong><br />
<div id="dailysummary"><br />Daily Summary: ' . $cond['summary'] . '<br /><br /></div>
<table style="border:1px solid #ccc">
<tr>
<td>
High
</td>
<td>
'.round($cond['temperatureMax']).'
</td>
</tr>
<tr>
<td>
Low
</td>
<td>
'.round($cond['temperatureMin']).'
</td>
</tr>
<tr>
<td>
Chance of precip.
</td>
<td>
'.round($cond['precipProbability']).'%
</td>
</tr>
<tr>
<td>
Wind speed
</td>
<td>
'.round($cond['windSpeed']).' mph
</td>
</tr>
<tr>
<td>
Dewpoint.
</td>
<td>
'.round($cond['dewPoint']).'
</td>
</tr>
<tr>
<td>
Humidity
</td>
<td>
'.round($cond['humidity']).'
</td>
</tr>
</table>
<br /><br />';
}
答案 0 :(得分:3)
构建变量而不是回显:
$html = '';
foreach(...) {
$html .= '... build html here...';
}
或保留当前代码,并使用输出缓冲:
ob_start();
foreach(...) {
echo '... html goes here ...';
}
$html = ob_get_clean();
这样做没有正确/错误的方法。无论哪种方式,你都会在内存中结束一大堆html。
答案 1 :(得分:0)
这样做:
$content = "";
foreach ($response['daily']['data'] as $cond) {
$content .= '<br /><br /><strong>' . date('l, F j, Y', $cond['time']) .'</strong><br />
[...]
您将创建一个空字符串,并在每个循环中添加更多文本。
答案 2 :(得分:0)
像这样 - 使用.=
运算符:
$content = '';
foreach ($response['daily']['data'] as $cond) {
$content .= '<br /><br /><strong>' . date('l, F j, Y', $cond['time']).'</strong><br />
<div id="dailysummary"><br />Daily Summary: ' . $cond['summary'] . '<br /><br /></div>
<table style="border:1px solid #ccc">
... ';
}
print $content;