开发者

codeigniter display a random record from DB every page load

I have a query that gets one random record from a database table and saves it in, $banner_text I am having trouble showing that banner text in my template though.

Below is my code for MY_Controller which all my other controllers extend, I wanting to use this to send $banner_text to whatever template/layout is being built.

class MY_Controller extends Controller {

    function __construct() {
        parent::Controller();
        $this->load->model('banners_model');
        //$this->output->enable_profiler(TRUE);
        $this->template->set_theme('moovjob');
        $this->template->set_layout('main');
        //$this->banners_model->get_header(); 

        //$this->template->title('Some title');
        //$this->template->开发者_运维问答build('main');
    }
}


To load a variable globally in all views loaded by this controller ( or child controller ) use...

$this->load->vars($data);

( Part of the Loader Class )

so in your case.

class MY_Controller extends Controller {

function __construct() {
    parent::Controller();
    $this->load->model('banners_model');
    //$this->output->enable_profiler(TRUE);
    $this->template->set_theme('moovjob');
    $this->template->set_layout('main');
    $data['banner_text'] = $this->banners_model->get_header(); 
    $this->load->vars($data);

    //$this->template->title('Some title');
    //$this->template->build('main');
}

Now $banner_text will be available in all views loaded by this controller. }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜