简体   繁体   English

如何将不同模型的两个不同ID从控制器传递到视图

[英]How to pass two different ids of different models from a controller to a view

I am having an issue with passing two ids in blog posts. 我在博客文章中传递两个ID时遇到问题。 The first id displays a single post and the second one counts the number of posts. 第一个ID显示一个帖子,第二个ID计算帖子的数量。 Each works well separately but on integrating the two, it selects a wrong post. 每个都可以单独很好地工作,但是在将两者整合在一起时,它选择了错误的帖子。

This is my code for the first model that fetches the comments 这是我获取注释的第一个模型的代码

class News_model extends CI_Model {    
     function get_comment($id)
     {
         $this->db->select('comments.*,users.username');
         $this->db->from('comments');
         $this->db->join('users','users.id = comments.user_id', 'left');
         $this->db->where('post_id',$id);
         $this->db->order_by('date_added','asc');
         $query = $this->db->get();
         return $query->result_array();
     }
} 

This is the model that fetches the posts 这是获取帖子的模型

class News_model extends CI_Model {  
  function get_one_news($news_id)
     {
        $this->db->select('*, news.id as id');
         $this->db->from('news');
         $this->db->join('users' , 'users.id = news.user_id');
         $this->db->where('news.id' , $news_id);
         $query = $this->db->get();
         return $query->first_row('array');
     } 
  function latestnews()     {
        $this->db->select("
        news.*,users.*,comments.*,COUNT(comments.post_id) AS num_comments");
         $this->db->from('news');
         $this->db->join('users' , 'users.id = news.user_id'); 
         $this->db->join('comments' , 'comments.post_id = news.id');
         $this->db->group_by('comments.post_id');
         $this->db->order_by('news.date_added','desc');     
         $this->db->limit(4);
         $query = $this->db->get();
         return $query->result_array(); 
    }
 }

And here is my controller 这是我的控制器

function view($id)
     {       
        $data['news'] = $this->news_model->get_one_news($id);
        $data["latest_news"] = $this->news_model->latestnews();         
        $data['comments'] = $this->m_comment->get_comment($id);    

        $data['content'] = 'single'; // template part
        $this->load->view('includes/template',$data);
     }

And finally the view file 最后是查看文件

<div id="sidebar" class="col-md-3 fix-left">
                <!---- Start Widget ---->
                <div class="widget wid-vid">
                    <div class="heading">
                        <h4>Latest News</h4>
                    </div>
                    <div class="content">

                        <div class="tab-content">

                            <div id="most" class="tab-pane fade in active">


                                <div class="post wrap-vid">
                                <?php if(count($latest_news)) { 
                             foreach($latest_news as $l_news){ ?>
                                    <div class="zoom-container">
                                        <div class="zoom-caption">
                                        <a href="<?php echo base_url()?bulletins/view/<?php echo $l_news['id']?>"></a>
                                        </div>
                                    <img src="<?php echo base_url('assets/images/'.$l_news['image'])?>"
 style="height:80px;width:100px;"/>

                                    </div>
                                    <div class="wrapper">
                                    <h5 class="vid-name"><a href="<?php echo  base_url('bulletins/view/'.$l_news['id'])?>"><?php echo 
 substr(strip_tags($l_news['title']), 0, 15).'..';?></a></h5>
                                            <div class="info">  
                                            <h6>By <a href="#"><?php echo $l_news['username']?></a></h6>
                                            <span><i class="fa fa-calendar"></i><?php echo date( 'F jS, Y' , strtotime($l_news['date_added']))?></span>
                                             <span><i class="fa fa-comment-o">/i><?php echo
 $l_news['num_comments'];?></span>                                          
                                            &nbsp;
                                            </div>
                                        &nbsp;
                                    </div>
                                        <?php }}?>

                                </div>

                                </div>

                        </div>
                    </div>
                </div> 

Please check below mentioned solution. 请检查以下提到的解决方案。 You have error in your select cause . select cause有误。 When you want data from multiple table in CI and if columnName is same than CI query builder will overwrite value of that column. 如果要从CI中的多个表中获取数据,并且columnName与CI相同,则查询构建器将覆盖该列的值。 In this case you need to give alias to columnName . 在这种情况下,您需要为columnName赋予alias Please check below. 请在下面检查。

$this->db->select('news.id as newsId,users.id as userId,news.*,users.*,comments.*, COUNT(comments.post_id) as num_comments');

And in your view file you need to change below code. 并且在您的视图文件中,您需要更改以下代码。

Find $l_news['id'] and replace with $l_news['newsId'] 找到$l_news['id']并替换为$l_news['newsId']

This will resolve your issue. 这样可以解决您的问题。 Let me know if it not works for you. 让我知道它是否不适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM