没有显示正确的输出视图

时间:2016-09-25 00:54:28

标签: codeigniter

我有两个名为forum和forum_category的数据库表。

在论坛表" General"和"蔬菜"

在forum_categories上有两个论坛类别,一般是一般的,一个是蔬菜。

但由于某种原因,我的论坛蔬菜显示多个类别应该只显示一个。

  

我的问题是如何才能显示正确的类别   正确的论坛。

注意: 它在print_r图片中显示正确的结果

预览

enter image description here

查看

<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <?php foreach ($forums as $forum) {?>
            <div class="panel panel-custom">
                <div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
                <div class="panel-body">
                    <?php foreach ($forum['categories'] as $category) {?>
                    <table class="table">
                    <tbody>                 
                    <tr>
                    <td><?php echo $category['category_name'];?></td>
                    </tr>
                    </tbody>
                    </table>
                    <?php }?>
                </div>
            </div><!-- Panel -->
        <?php }?>
    </div>
</div>

模型功能

public function get_result_categories($forum_parent_id) {
    $this->db->select('*');
    $this->db->from($this->db->dbprefix . 'forum_category');
    $this->db->where('forum_parent_id', $forum_parent_id);
    $query = $this->db->get();
    return $query->result_array();
}

public function get_results() {
    $this->db->select('*');
    $this->db->from($this->db->dbprefix . 'forum');
    $query = $this->db->get();
    return $query->result_array();
}

印刷结果

enter image description here

控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()
    {
        $results = $this->get_results();

        foreach ($results as $result) {

            $categories_results = $this->get_result_categories($result['forum_id']);

            echo "<pre>";
            print_r($categories_results);
            echo "</pre>";

            foreach ($categories_results as $category_result) {
                $categories[] = array(
                    'category_name' => $category_result['category_name']
                );
            }

            $data['forums'][] = array(
                'forum_name' => $result['forum_name'],
                'categories' => $categories
            ); 
        }

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

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


    public function get_result_categories($forum_parent_id) {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'forum_category');
        $this->db->where('forum_parent_id', $forum_parent_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_results() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'forum');
        $query = $this->db->get();
        return $query->result_array();
    }

}

1 个答案:

答案 0 :(得分:0)

认为我现在有解决方案

解决方案

放置$categories = array();

以上$categories_results做了伎俩

public function index()
{
    $results = $this->get_results();

    foreach ($results as $result) {

        $categories = array();

        $categories_results = $this->get_result_categories($result['forum_id']);

        foreach ($categories_results as $category_result) {

            $categories[] = array(
                'category_name' => $category_result['category_name']
            );
        }

        $data['forums'][] = array(
            'forum_name' => $result['forum_name'],
            'categories' => $categories
        ); 
    }

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

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

图像

enter image description here