开发者

problem with php and template

开发者 https://www.devze.com 2023-02-14 20:35 出处:网络
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 t

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?


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


Change

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

to

 $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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消