Php 返回查询中的类别和论坛?

Php 返回查询中的类别和论坛?,php,sql,codeigniter,Php,Sql,Codeigniter,我目前正在CodeIgniter内部开发一个简单的论坛来处理PHP,我遇到了一个问题,尽管是初级问题 我的论坛索引由多个类别组成,每个类别都有几个分配给它们的论坛。我想显示每个类别的标题,然后返回下面的每个论坛 下面是我目前返回数据的模型: function get_top_level_forums() { $this->db->select('id, title'); $this->db->from('forum_category'); $q

我目前正在CodeIgniter内部开发一个简单的论坛来处理PHP,我遇到了一个问题,尽管是初级问题

我的论坛索引由多个类别组成,每个类别都有几个分配给它们的论坛。我想显示每个类别的标题,然后返回下面的每个论坛

下面是我目前返回数据的模型:

function get_top_level_forums()

{

    $this->db->select('id, title');
    $this->db->from('forum_category');
    $query = $this->db->get();

    if ($query->num_rows() > 0)

    {

        $categories = $query->result();

        foreach($categories as $category)

        {

            $this->db->select('id, title, description');
            $this->db->from('forum');
            $this->db->where('parent_id', 0);
            $this->db->where('category_id', $category->id);
            $query = $this->db->get();

            $forums = $query->result();

        }

        return $categories;

    }

}
我正在努力研究如何在单个数组中返回所有数据。我知道这是一个新手问题,但我已经问了一个小时了,看不到任何曙光

我知道如何使用控制器和视图中的数据。我只是停留在模型部分

谢谢


:)

将内部查询的结果放入循环内的数组中,然后返回该数组。

使用数组代替变量存储结果

$forums[] = $query->result();
完整示例:

$forums = array();
foreach($categories as $category)

        {

            $this->db->select('id, title, description');
            $this->db->from('forum');
            $this->db->where('parent_id', 0);
            $this->db->where('category_id', $category->id);
            $query = $this->db->get();

            $forums[] = $query->result();

        }
        //$forums now contains all the query results

使现代化 访问您可以使用的变量

$forums[$forumindex][0] -> id;

伟大的这将返回论坛。我如何返回类别标题并显示它?