1

On my Welcome controller I have a function called content_top which loads function $this->$part[0]($setting_info); in this case $this->slideshow($setting_info); Then it should display that slideshow functions view in that data array.

When I have refreshed my page I get a error

A PHP Error was encountered
Severity: 4096
Message: Object of class CI_Loader could not be converted to string
Filename: common/content_top.php
Line Number: 7
Backtrace:
File: C:\wamp\www\cms\application\views\common\content_top.php
Line: 7
Function: _error_handler
File: C:\wamp\www\cms\application\controllers\Welcome.php
Line: 51
Function: view
File: C:\wamp\www\cms\application\controllers\Welcome.php
Line: 19
Function: content_top
File: C:\wamp\www\cms\index.php
Line: 292
Function: require_once

And

A PHP Error was encountered
Severity: 4096
Message: Object of class CI_Loader could not be converted to string
Filename: common/welcome_message.php
Line Number: 3
Backtrace:
File: C:\wamp\www\cms\application\views\common\welcome_message.php
Line: 3
Function: _error_handler
File: C:\wamp\www\cms\application\controllers\Welcome.php
Line: 20
Function: view
File: C:\wamp\www\cms\index.php
Line: 292
Function: require_once

Question Is there any way to when I have a function in array to be able to make it display the view with out throwing error.

<?php

class Welcome extends CI_Controller {

    public $data = array();

    public function __construct() {
        parent::__construct();
        $this->load->model('design/model_layout');
        $this->load->model('extension/model_module');
        $this->load->model('design/model_banner');
        $this->load->model('tool/model_image');

        $this->load->library('image_lib');
    }

    public function index() {
        $this->load->view('common/header');
        $data['content_top'] = $this->content_top();
        $this->load->view('common/welcome_message', $data);
        $this->load->view('common/footer');
    }

    public function content_top() {
        if ($this->uri->segment(1)) {
            $route = $this->uri->segment(1) .'/'. $this->uri->segment(2);
        } else {
            $route = 'common/home';
        }

        $layout_id = $this->model_layout->get_layout($route);

        $data['modules'] = array();

        $modules = $this->model_layout->get_layout_modules($layout_id, 'content_top');

        foreach ($modules as $module) {
            $part = explode('.', $module['code']);

            if (isset($part[1])) {
                $setting_info = $this->model_module->get_module($part[1]);

                if ($setting_info) {
                    $data['modules'][] = $this->$part[0]($setting_info);

                }
            }
        }


        return $this->load->view('common/content_top', $data);
    }

    public function slideshow($setting_info) {
        static $module = 0;

        $data['banners'] = array();

        $results = $this->model_banner->get_banner($setting_info['banner_id']);

        foreach ($results as $result) {
            $data['banners'][] = array(
                'banner_image' => $this->model_image->resize($result['banner_image'], $setting_info['width'], $setting_info['height'])
            );
        }

        $data['module'] = $module++;

        $data['resize_errors'] = $this->image_lib->display_errors();

        return $this->load->view('module/slideshow', $data);
    }
}
16
  • remove True in here $string = $this->load->view('common/content_top', $data, true); as well as this will not execute return $string; Commented Oct 18, 2015 at 7:50
  • @Abdulla now throws error Object of class CI_Loader could not be converted to string Commented Oct 18, 2015 at 7:51
  • You wrote two functions in two different methods !!, When CI load only one for the view Commented Oct 18, 2015 at 7:52
  • I have removed all the true from view() but still get blank page just as you can see in image slideshow part not showing on row div. Commented Oct 18, 2015 at 8:01
  • To get data to index function, you have to return data not to load view. This should be $this->load->view('common/content_top', $data); change to return $data Commented Oct 18, 2015 at 8:04

4 Answers 4

5

In the CI when you loading the view and printing as a string then need to pass the third parameter as 'TRUE'.

<?php echo $this->load->view('headers/menu', '', TRUE);?>
Sign up to request clarification or add additional context in comments.

Comments

4

Solved

On the controller I had to change a couple things around

I also have had to use a template layout to minimize the views

But on content_top and sideshow function I had to use return and true

return $this->load->view('folder/name', $this->data, TRUE);

Controller

<?php

class Home extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->model('design/model_layout');
    $this->load->model('extension/model_module');
    $this->load->model('design/model_banner');
    $this->load->model('tool/model_image');

    $this->load->library('image_lib');
}

public function index() {
    $this->data['title'] = 'Home';

    $this->data = array(
        'column_left' => $this->column_left(),
        'column_right' => $this->column_right(),
        'content_bottom' => $this->content_bottom(),
        'content_top' => $this->content_top(),
        'page' => 'common/home'
    );

    $this->load->view('common/template', $this->data);
}

public function column_left() {
    $route = 'common/home';
    
    $layout_id = $this->model_layout->get_layout($route);

    $this->data['modules'] = array();

    $modules = $this->model_layout->get_layout_modules($layout_id, 'column_left');

    foreach ($modules as $module) {
        $part = explode('.', $module['code']);
        
        $setting_info = $this->model_module->get_module($part[1]);
        
        $this->data['modules'][] = array (
            'module_name' =>  $this->$part[0]($setting_info)
        );
    }

    return $this->load->view('common/column_left', $this->data, TRUE);
}

public function column_right() {
    $route = 'common/home';
    
    $layout_id = $this->model_layout->get_layout($route);

    $this->data['modules'] = array();

    $modules = $this->model_layout->get_layout_modules($layout_id, 'column_right');

    foreach ($modules as $module) {
        $part = explode('.', $module['code']);
        
        $setting_info = $this->model_module->get_module($part[1]);
        
        $this->data['modules'][] = array (
            'module_name' =>  $this->$part[0]($setting_info)
        );
    }

    return $this->load->view('common/column_right', $this->data, TRUE);
}


public function content_bottom() {
    $route = 'common/home';
    
    $layout_id = $this->model_layout->get_layout($route);

    $this->data['modules'] = array();

    $modules = $this->model_layout->get_layout_modules($layout_id, 'content_bottom');

    foreach ($modules as $module) {
        $part = explode('.', $module['code']);
        
        $setting_info = $this->model_module->get_module($part[1]);
        
        $this->data['modules'][] = array (
            'module_name' =>  $this->$part[0]($setting_info)
        );
    }

    return $this->load->view('common/content_bottom', $this->data, TRUE);
}

public function content_top() {
    $route = 'common/home';
    
    $layout_id = $this->model_layout->get_layout($route);

    $this->data['modules'] = array();

    $modules = $this->model_layout->get_layout_modules($layout_id, 'content_top');

    foreach ($modules as $module) {
        $part = explode('.', $module['code']);
        
        $setting_info = $this->model_module->get_module($part[1]);
        
        $this->data['modules'][] = array (
            'module_name' =>  $this->$part[0]($setting_info)
        );
    }
    return $this->load->view('common/content_top', $this->data, TRUE);
}

/*

    Add function for modules that are enabled for this page

*/

public function slideshow($setting_info) {
    static $module = 0;

    $this->data['banners'] = array();

    $results = $this->model_banner->get_banner($setting_info['banner_id']);

    foreach ($results as $result) {
        $this->data['banners'][] = array(
            'banner_image' => $this->model_image->resize($result['banner_image'], $setting_info['width'], $setting_info['height'])
        );
    }

    $this->data['module'] = $module++;

    return $this->load->view('module/slideshow', $this->data, TRUE);
}

}

Template View

<?php $this->load->view('common/header');?>

<?php $this->load->view($page);?>

<?php $this->load->view('common/footer');?>

Content Top View

<?php foreach ($modules as $module) {?>
    <?php echo $module['module_name'];?>
<?php }?>

Home View

<div class="container">
    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <?php echo $content_top;?>
        </div>
    </div>
</div>

Proof Working

enter image description here

Comments

2

Just remove the echo on view page when you are using CI new versions.

<?php $this->load->view('headers/menu');?>

https://stackoverflow.com/a/40675633/2790502

Comments

1

This happened to me when I did this:

$model = $this->load->model('user_model);

$data = [
    'model' => $model
];    

instead of this:

$this->load->model('user_model);

$model = $this->user_model->edit();

$data = [
    'model' => $model
];    

then when I echoed out the $model, it gave me the error. So I tried to print_r then it gave me a very long array until I realize my mistake. Hope it helps.

1 Comment

Just trying to help others who have my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.