【问题标题】:How can I get category name of post in news module from category module in CodeIgniter HMVC如何从 CodeIgniter HMVC 的类别模块中获取新闻模块中帖子的类别名称
【发布时间】:2014-04-25 14:59:33
【问题描述】:

我想获取新闻帖子的类别名称。我可以轻松获取 category id 但我想使用 category Id 或其他方法获取 category name。我该怎么做?

这就是我在控制器中显示我的博客文章的方式

function post($slug = FALSE)
{
    if (isset($slug)) {
        $data['query'] = $this->mdl_blogs->get_where_slug($slug);

        $data['view_file'] = "blog_view";
        $this->load->module('template');
        $this->template->public_one_col($data);
    }



}

模型中

function get_where_slug($slug){
 $table = $this->get_table();
$this->db->where('news_slug', $slug);
$query=$this->db->get($table);
return $query;
}
 in view
 <?php
                foreach ($query->result() as $row) {

                $data['news_title'] = $row->news_title;
                $news_body = $row->news_body;
                $news_slug = $row->news_slug;
                $category_id = $row->category_id;


                //$data['category_name'] = $row->category_name;
                //$news_category = $row->news_category;
                    ?>

<h2><a href="<?php echo   (base_url().'blogs/post/'.$news_slug) ;?>">
 <?php echo $data['news_title'];?></a></h2>
                <p><?php echo $news_body;?></p>
                <p><?php echo $category_id;?></p>



                                    <?php
                    }
                    ?>
                    <?php
                        //echo Modules::run('comments');
                    ?>

这里我也想显示类别名称。我想从 categories 表中获取它

这是我的桌子

in News table 
news_id
category_id
news_slug
......

in categories 
category_id
category_name
category_slug

【问题讨论】:

    标签: php mysql sql codeigniter hmvc


    【解决方案1】:

    你需要在你的模型中加入你的类别表

    function get_where_slug($slug){
    $table = $this->get_table();
    $query=$this->db
                ->select('n.*,c.category_name,c.category_slug')
                ->from($table.' n')
                ->join('category c','n.category_id=c.category_id','LEFT')
                ->where('n.news_slug', $slug)
                ->get();
    return $query;
    }
    

    在您看来,您可以将 foreach 循环中的类别名称设为

    $category_name = $row->category_name;
    

    category_slug也一样

    【讨论】:

    • 感谢 Khalid 我这样做了,所以没有成功。你拯救了我的一天。但是你在一个地方弄错了——这个;和 $table.$data['query'] =$this->db->select('category_name'); 之后的那个 n $data['query'] =$this->db->from('categories'); $data['query'] =$this->db->join('news', 'categories.category_id = news.category_id'); $data['query'] =$this->db->get();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多