php和模板的问题

发布于 2024-10-20 08:18:51 字数 2423 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不气馁 2024-10-27 08:18:51

我认为正确的代码是:

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.";
        $return="";

        $columns=mysql_query('SELECT * FROM `column` ORDER BY id');
        while($column=mysql_fetch_array($columns))
        {
            $return .= '<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))
            {
                $return .= '
                <div class="dragbox" id="item'.$widget['id'].'">
                    <h2>'.$widget['title'].'</h2>
                        <div class="dragbox-content" ';
                if($widget['collapsed']==1)
                    $return .= 'style="display:none;" ';
                $return.= '>
                            '.$dummy.'
                        </div>
                </div>';
            }               
            $return .= '</div>';
        }
        // return the value
        return $return;
    }

}

在您的模板文件中:

$build_panel = new build(); 
$file = str_replace('{main_panel_window}', $build_panel->panel(), $file);

I think the right code is:

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.";
        $return="";

        $columns=mysql_query('SELECT * FROM `column` ORDER BY id');
        while($column=mysql_fetch_array($columns))
        {
            $return .= '<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))
            {
                $return .= '
                <div class="dragbox" id="item'.$widget['id'].'">
                    <h2>'.$widget['title'].'</h2>
                        <div class="dragbox-content" ';
                if($widget['collapsed']==1)
                    $return .= 'style="display:none;" ';
                $return.= '>
                            '.$dummy.'
                        </div>
                </div>';
            }               
            $return .= '</div>';
        }
        // return the value
        return $return;
    }

}

and in your template file:

$build_panel = new build(); 
$file = str_replace('{main_panel_window}', $build_panel->panel(), $file);
只为守护你 2024-10-27 08:18:51

更改

 $file = str_replace('{main_panel_window}', $build_panel, $file);

 $file = str_replace('{main_panel_window}', $build_panel.panel(), $file);

Change

 $file = str_replace('{main_panel_window}', $build_panel, $file);

to

 $file = str_replace('{main_panel_window}', $build_panel.panel(), $file);
椵侞 2024-10-27 08:18:51

问题正是错误所说的那样:您试图在调用 str_replace 时使用 build 对象,例如字符串。

也许你需要字符串转换,或者你可能需要重新思考为什么你'将 build 对象传递给 str_replace

如果您想让 panel() 返回一个字符串,那么让它返回一个字符串而不是直接 echoing,然后编写:

$file = str_replace('{main_panel_window}', $build_panel.panel(), $file);

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 to str_replace.

Perhaps you need a string conversion, or perhaps you need to re-think why you're passing a build object to str_replace.

If you meant for panel() to return a string, then make it return a string instead of echoing directly, then write:

$file = str_replace('{main_panel_window}', $build_panel.panel(), $file);
〗斷ホ乔殘χμё〖 2024-10-27 08:18:51

您无法按照此代码的编写方式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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文