php和模板的问题
PHP代码:
class build {
function panel() {
$dummy="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum velit ultricies orci pharetra elementum. In massa mauris, varius sed tempus a, iaculis sed erat. Ut sollicitudin tellus mollis arcu laoreet semper. Suspendisse ut felis odio. Aliquam auctor, tortor sit amet suscipit elementum, nunc ante dictum lectus, ac accumsan justo nunc sed velit. Sed sollicitudin varius tortor vitae varius. Aliquam interdum, nisl consectetur laoreet commodo, metus massa sagittis nisl, non venenatis lacus mi nec tortor. Ut malesuada auctor dolor, id pulvinar est malesuada sed. Aliquam sed posuere orci. Proin porttitor euismod condimentum. Integer suscipit nibh nec augue facilisis ut commodo nisi ornare. Nam sed mauris vitae justo convallis placerat. Curabitur viverra, ipsum id volutpat sollicitudin, mi nisi condimentum nulla, nec dapibus velit libero eget orci. Nam purus lectus, imperdiet pharetra pulvinar ac, sodales sit amet sem. Ut vel mollis ante. Vivamus consectetur varius risus eu hendrerit. Sed scelerisque euismod leo, quis accumsan justo venenatis eu. Ut risus lorem, aliquet id fermentum nec, auctor ut enim. Ut pretium elementum turpis vel dignissim.";
$columns=mysql_query('SELECT * FROM `column` ORDER BY id');
while($column=mysql_fetch_array($columns))
{
echo '<div class="column" id="column'.$column['id'].'" >';
$items=mysql_query("SELECT * FROM widgets WHERE column_id='".$column['id']."' AND user='".$_COOKIE['id']."' ORDER BY sort_no");
while($widget=mysql_fetch_array($items))
{
echo '
<div class="dragbox" id="item'.$widget['id'].'">
<h2>'.$widget['title'].'</h2>
<div class="dragbox-content" ';
if($widget['collapsed']==1)
echo 'style="display:none;" ';
echo '>
'.$dummy.'
</div>
</div>';
}
echo '</div>';
}
}
}
$build_panel = new build();
并放入模板:
$file = str_replace('{main_panel_window}', $build_panel, $file);
错误:类构建的对象无法转换为字符串......第47行
如何解决问题?
Php code:
class build {
function panel() {
$dummy="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum velit ultricies orci pharetra elementum. In massa mauris, varius sed tempus a, iaculis sed erat. Ut sollicitudin tellus mollis arcu laoreet semper. Suspendisse ut felis odio. Aliquam auctor, tortor sit amet suscipit elementum, nunc ante dictum lectus, ac accumsan justo nunc sed velit. Sed sollicitudin varius tortor vitae varius. Aliquam interdum, nisl consectetur laoreet commodo, metus massa sagittis nisl, non venenatis lacus mi nec tortor. Ut malesuada auctor dolor, id pulvinar est malesuada sed. Aliquam sed posuere orci. Proin porttitor euismod condimentum. Integer suscipit nibh nec augue facilisis ut commodo nisi ornare. Nam sed mauris vitae justo convallis placerat. Curabitur viverra, ipsum id volutpat sollicitudin, mi nisi condimentum nulla, nec dapibus velit libero eget orci. Nam purus lectus, imperdiet pharetra pulvinar ac, sodales sit amet sem. Ut vel mollis ante. Vivamus consectetur varius risus eu hendrerit. Sed scelerisque euismod leo, quis accumsan justo venenatis eu. Ut risus lorem, aliquet id fermentum nec, auctor ut enim. Ut pretium elementum turpis vel dignissim.";
$columns=mysql_query('SELECT * FROM `column` ORDER BY id');
while($column=mysql_fetch_array($columns))
{
echo '<div class="column" id="column'.$column['id'].'" >';
$items=mysql_query("SELECT * FROM widgets WHERE column_id='".$column['id']."' AND user='".$_COOKIE['id']."' ORDER BY sort_no");
while($widget=mysql_fetch_array($items))
{
echo '
<div class="dragbox" id="item'.$widget['id'].'">
<h2>'.$widget['title'].'</h2>
<div class="dragbox-content" ';
if($widget['collapsed']==1)
echo 'style="display:none;" ';
echo '>
'.$dummy.'
</div>
</div>';
}
echo '</div>';
}
}
}
$build_panel = new build();
and putting in template :
$file = str_replace('{main_panel_window}', $build_panel, $file);
Error: Object of class build could not be converted to string in .... on line 47
how solve the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为正确的代码是:
在您的模板文件中:
I think the right code is:
and in your template file:
更改
为
Change
to
问题正是错误所说的那样:您试图在调用
str_replace
时使用build
对象,例如字符串。也许你需要字符串转换,或者你可能需要重新思考为什么你'将
build
对象传递给str_replace
。如果您想让
panel()
返回一个字符串,那么让它返回一个字符串而不是直接echo
ing,然后编写:The problem is just what the error says it is: that you're trying to use a
build
object like a string in the call tostr_replace
.Perhaps you need a string conversion, or perhaps you need to re-think why you're passing a
build
object tostr_replace
.If you meant for
panel()
to return a string, then make it return a string instead ofecho
ing directly, then write:您无法按照此代码的编写方式
str_replace()
。我认为您打算对 $build_panel->panel() 的输出进行操作,但该函数直接回显字符串,而不是构建模板输出并将其作为字符串返回。因此,要么在某些输出中使用wrap
panel()
缓冲 或让它连接并返回一个字符串。You can't
str_replace()
the way this code is written. I think you're intending to operate on the output of$build_panel->panel()
, but the function echos strings directly rather than building template output and returning it as a string.So, either use wrap
panel()
in some output buffering or have it concatenate and return a string.