代码之家  ›  专栏  ›  技术社区  ›  HemalHerath

如何从两个函数向同一视图传递数据?

  •  0
  • HemalHerath  · 技术社区  · 6 年前

    我的函数如下

    public function view($slug = NULL){
    
            $data['book'] = $this->Book_Model->get_books($slug);
            $data['categories'] = $this->Category_Model->get_categories();
    
            //current book view
            $book_id = $data['book']['book_id'];
            echo "current book-".$data['book']['book_id']." ";
    
            if(empty($data['book'])) {
                show_404();
            }
    
            $this->add_views($book_id);
            $this->get_viewed_books($book_id);
    
            $this->load->view('templates/header', $data);
            $this->load->view('books/view', $data);
            $this->load->view('templates/footer');
    
    
        }
    

    查看\u书籍 功能如下

    public function get_viewed_books($book_id) {
    
    $data['results'] = $this->Book_Model->get_viewed_books($book_id);
    
    }
    

    $data['results'] $this->load->view('books/view', $data);

    1 回复  |  直到 6 年前
        1
  •  1
  •   u_mulder    6 年前

    分配结果 get_viewed_books 一些钥匙 $data

    $this->add_views($book_id);
    $data['viewed_books'] = $this->get_viewed_books($book_id); // here
    
    $this->load->view('templates/header', $data);
    $this->load->view('books/view', $data);
    $this->load->view('templates/footer');
    

    现在,在模板中你有一个 viewed_books

    当然还有你的 get_viewed_books($book_id) 应该 返回结果 ,而不是分配给某个未知的 $数据

    public function get_viewed_books($book_id) {
        return $this->Book_Model->get_viewed_books($book_id);
    }