Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在codeigniter中获取递增子元素数据库_Mysql_Codeigniter_Activerecord_Array Merge - Fatal编程技术网

Mysql 在codeigniter中获取递增子元素数据库

Mysql 在codeigniter中获取递增子元素数据库,mysql,codeigniter,activerecord,array-merge,Mysql,Codeigniter,Activerecord,Array Merge,我试图从包含交叉引用的数据库表中获取一系列ID——每个元素(一个“主题”)都包含一列“父主题”,该列位于同一个表中。给定一个父主题,我想构建一个包含所有子主题的数组,这些子主题将其作为父主题,然后是这些主题的所有子主题,等等 这看起来并不难,但作为一个自学成才的程序员,我觉得我使用了所有错误的工具。尤其是merge-array()和var\u dump()部分感觉不对,我不确定整体方法。我应该用什么替换这些元素 function get_subtopics($parent_topic) {

我试图从包含交叉引用的数据库表中获取一系列ID——每个元素(一个“主题”)都包含一列“父主题”,该列位于同一个表中。给定一个父主题,我想构建一个包含所有子主题的数组,这些子主题将其作为父主题,然后是这些主题的所有子主题,等等

这看起来并不难,但作为一个自学成才的程序员,我觉得我使用了所有错误的工具。尤其是
merge-array()
var\u dump()
部分感觉不对,我不确定整体方法。我应该用什么替换这些元素

function get_subtopics($parent_topic)
{
    //returns an array of subtopics minus the first 
    $all_subs = array();

    $query = $this->db->get_where('topics', array('parent_topic' => $parent_topic));

    $subs = $query->result_array();
    $resubs = array();
    $query->free_result();
    //push subs to all_subs

    //while the subs array has members, find their child
    while (count($subs)>0) {
        foreach ($subs as $s) {
            $query = $this->db->get_where('topics', array('parent_topic' => $s['id']));
            $resubs = array_merge($resubs, $query->result_array());
            $query->free_result();
        }
        $all_subs = array_merge($all_subs, $resubs);
        var_dump($resubs);
    }


    //Returns an array of ids
    return $all_subs;
}
编辑:
这样做的目的是形成一个主题的“池”,从中可以为随机生成器提取问题-我试图将所有子主题放在一个数组中,没有树结构来区分它们。指定父主题(如“数学”)的用户应获得“代数”、“代数:求积”或“微积分”等数学子主题的混合,从中可以得出问题。希望这能澄清一点。

有两种方法可以做到这一点,一种是从数据库中获取所有记录,另一种是使用下面的php递归函数构建树结构

//Build menu array containing links and subs
$items = Array( 
    //highest level
    'cms' => Array(
        'title' => 'CMS',
        //Array containing submenu items for cms
        'subs' => Array(
            'intro-to-cms' => Array('title' => 'Intro to CMS'),
            'specific-cms' => Array('title' => 'Specific CMS'),
            'installing-a-cms' => Array('title' => 'Installing a CMS')
        ),
    )
);

//Display the menu
echo navlinks($items, $page);

/**
 * Recursive function creates a navigation out of an array with n level children
 * @param type $items
 * @return string containing treestructure
 */
function navlinks($items, $page=false)
{
  $html = '<ul>';
  foreach ($items AS $uri => $info) {
    //Check if the pagename is the same as the link name and set it to current when it is
    $html .= '<li'.($info['title'] == $page ? ' class="current"' : '').'>';
    echo '  <a href="' . $uri . '">' . $info['title'] . '</a>';
    //If the link has a sub array, recurse this function to build another list in this listitem
    if (isset($info['subs']) && is_array($info['subs'])) {
      $html .= navlinks($info['subs']);
    }
    $html .= '</li>';
  }
  $html .= '</ul>';
  return $html;
}
//构建包含链接和子菜单的菜单数组
$items=数组(
//最高级别
“cms”=>数组(
“标题”=>“CMS”,
//包含cms子菜单项的数组
'subs'=>数组(
“cms简介”=>数组('title'=>“cms简介”),
“特定cms'=>数组('title'=>特定cms'),
'安装-a-cms'=>阵列('title'=>'安装cms')
),
)
);
//显示菜单
echo导航链接($items,$page);
/**
*递归函数从具有n级子级的数组中创建导航
*@param type$items
*@包含treestructure的返回字符串
*/
函数导航链接($items,$page=false)
{
$html=“
    ”; foreach($uri=>$info形式的项目){ //检查pagename是否与链接名相同,并在指定时将其设置为current $html.=''; 回声'; //如果链接有子数组,则递归此函数以在此listitem中构建另一个列表 if(isset($info['subs'])和&is_数组($info['subs'])){ $html.=navlinks($info['subs']); } $html.=''; } $html.='
'; 返回$html; }
为了只在1个父级及其底层子级上进行筛选,您需要事先进行一个相当复杂的查询,就像前面关于stackoverflow的评论中所解释的那样。(以下链接)


我很欣赏您的详细介绍,但不幸的是,构建树结构并不是我的目标-我从一个“池”中随机获取一系列问题,其中必须包含父项下的所有主题(例如,如果用户指示“数学”,他们将收到代数、计算等的混合问题),我将在编辑中指定。