Codeigniter加入不正确显示组

时间:2016-09-26 09:01:07

标签: codeigniter

我下面有这个模型函数,可以让我加入两个表。印刷结果是最重要的。

<?php

class Forum_model extends CI_Model {

    public function get_forums() {
        $this->db->select('f.*, fc.*');
        $this->db->from('forum_category fc', 'left');
        $this->db->join('forum f', 'f.forum_id = fc.category_forum_id', 'left');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }
}

当我查看我的视图时,它会像下面的图像一样显示

一个面板上应显示两个常规类别新闻休息室。出于某种原因,它会在自己的面板中显示两个常规类别。

  

问题:如何将两个类别一起显示?我试过了$this->db->group_by('fc.category_forum_id')

图片

enter image description here

控制器

<?php

class Home extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('reports/thread_analytics_model');
        $this->load->model('forum/forum_model');
    }

    public function index() {

        $results = $this->forum_model->get_forums();

        echo "<pre>";

        print_r($results);

        echo "</pre>";

        $data['forums'] = array();

        foreach ($results as $result) {
            $data['forums'][] = array(
                'forum_name' => $result['forum_name'],
                'category_name' => $result['category_name']
            ); 
        }

        $data['content'] = 'common/home_view';

        $this->load->view('theme/default/template_view', $data);
    }
}

查看

<?php foreach ($forums as $forum) {?>
    <div class="panel panel-home">
        <div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
        <div class="panel-body">
        <table class="table">
            <tbody>
                <tr>
                    <td><?php echo $forum['category_name'];?></td>
                </tr>
            </tbody>
        </table>
        </div>
    </div><!-- Panel -->
<?php }?>

印刷结果图片

enter image description here

2 个答案:

答案 0 :(得分:0)

您的方法可能的解决方案是:

class Forum_model extends CI_Model {

    public function get_forums() {

        $arrGroupedData = array();

        $this->db->select('f.*, fc.*');
        $this->db->from('forum_category fc');
        $this->db->join('forum f', 'f.forum_id = fc.category_forum_id', 'left');
        $query = $this->db->get();

        if ($query->num_rows() > 0) 
        {
            foreach($query->result_array() AS $arrCategory)
            {
                $arrGroupedData[$arrCategory['forum_id']][] = $arrCategory;
            }
            return $arrGroupedData;
        } else {
            return false;
        }
    }
}

但在我看来 - 你应该分开你的查询 1个查询获取论坛和1个查询以获取论坛中的所有类别

并将其粘贴在树形结构中

答案 1 :(得分:0)

你的例子

$forums = array();
    $results = array(

        array(
            'forum_name'    =>  'general',
            'category_name' =>  'news1'
        ),
        array(
            'forum_name'    =>  'general',
            'category_name' =>  'news2'
        ),
        array(
            'forum_name'    =>  'latter',
            'category_name' =>  'news3'
        ),


    );

foreach ($results as $result)
{
    if(in_array($result['forum_name'],$forums))
    {
        $array = array($result['category_name']);
        array_push($forums[$result['forum_name']],$array);
    }
    else
    {
        $forums[$result['forum_name']][] = array(
            'forum_name'    => $result['forum_name'],
            'category_name' => $result['category_name']
        ); 
    }

}

echo '<pre>';
print_r($forums);