将2个数据库结果传递到视图中

时间:2014-04-22 09:39:28

标签: php codeigniter templates content-management-system

我在将2个查询中的数据传递到我的视图时遇到了一些麻烦。 我有一个人们可以发表评论的博客系统,但我想在我的评论页面顶部显示博客文章。

为此,我想我只会从检索博客条目数据的查询中传递结果。

问题在于我无法从视图中访问数组。

到目前为止我的代码。

控制器

public function blog($page){
    $this->load->library('pagination');

    $this->load->model("model_get");

    $config['base_url'] = base_url('main/blog/');
    $config['total_rows'] = $this->model_get->getBlogcount();
    $config['per_page'] = 5;
    $this->pagination->initialize($config);

    $data["results"] = $this->model_get->getBlog($config['per_page'], $page, "desc");
    $this->load->view("content_blog", $data);
}

function comments($page){
    $this->load->library('pagination');

    $this->load->model("model_get");

    $config['base_url'] = base_url('main/comments/'.$this->uri->segment(3).'/');
    $config['total_rows'] = $this->model_get->getCommentcount();
    $config['per_page'] = 5;
    $config['uri_segment'] = 4;
    $this->pagination->initialize($config);
    $data["blog"] = $this->model_get->getBlog(1,6, 'asc');
    $data["results"] = $this->model_get->getComments($config['per_page'], $page);
    $this->load->view('content_comment', $data);
}

模型

function getBlog($limit, $start, $sort){
        $this->db->order_by("id", $sort); 
        $results = $this->db->limit($limit, $start)->get('blog');

        if ($results1) 
        {
            return $results->result();
        }else{

        return FALSE;
        }
    }
function getComments($limit, $start){

        $this->db->from('comments');
        $this->db->where('blog_id', $this->uri->segment(3));
        $this->db->limit($limit, $this->uri->segment(4));
        $this->db->order_by("id", "desc"); 
        $results = $this->db->get();

        if ($results) 
        {
            return $results->result();
        }else{

        return FALSE;
        }
    }

查看

foreach($results as $row){
        $text = $row->text;
        echo "<p>". $text ."</p>";
        $name = $this->model_get->getUserById($row->author);

        echo "<h4>". $name[0]->login ."</h4>";

    }

0 个答案:

没有答案