Question

i am passing function _homepage to index but it does not pass the $data['articles'] with it. can any one help me to solve this? this code is updated

      public function index(){
    $mydata = array();
    $this->mydata['menu'] = $this->Mdl_page->get_nested();
    $this->mydata['page'] = $this->Mdl_page->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
    count($this->mydata['page']) || show_404(current_url());

    //fetch page data
    $method = '_' . $this->mydata['page']->template;
    if (method_exists($this, $method)) {
        $this->$method();
    }
    else {
        log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
        show_error('Could not load template ' . $method);
    }
    $this->mydata['subview'] = $data['page']->template;
    //dump($data);
    $this->load->view('_main_layout', $this->mydata);

}

private function _page(){
    dump('Welcome from the page template');
}
private function _homepage(){
    $this->load->model('admin/Mdl_Articles');
    $this->db->limit(6);
    $this->mydata['articles'] = $this->Mdl_Articles->get();
    //dump($data['articles']);

}
private function _news_archive(){
    dump('Welcome from the newspage template');
}
Était-ce utile?

La solution

You should instead create a private array property

private mydata = array();

In your _homepage method, contents should be:

private function _homepage(){
    // ...
    // target your class scope property
    $this->mydata['articles'] = $this->Mdl_Articles->get();

and change your all $data variable to $this->mydata in index method and pass it to the view like this:

$this->load->view('_main_layout', $this->mydata);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top