限制函数在 php codeigniter 中递归的次数
Restricting the number of times a function recurses in php codeigniter
我想在 codeigniter 中为 select 父类别或其任何子类别生成一个多级分层 select 选项。我想将层次结构级别限制为 3。即该选项必须显示父级、其第一级子级和第二级子级,子类别缩进到父级的右侧。我没有问题遍历所有层次结构级别。但是我无法生成特定层次结构级别的 select 选项。在我的案例中,我需要三个级别的层次结构。谁能帮帮我。
这是我到目前为止尝试过的方法:
<?php
$category = $this->get_category();
function get_category() {
$this->db->order_by('parent_id', 'DESC');
$this->db->select('id, name, parent_id');
return $this->db->get('tbl_job_category')->result_array();
}
/*
*$category contains the following json data obtained from DB
$category = [{"id":"20","name":"MVC","parent_id":"16"},
{"id":"16","name":"Spring","parent_id":"14"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"6","name":"Electrical","parent_id":"5"},
{"id":"3","name":"Civil","parent_id":"5"},
{"id":"14","name":"java","parent_id":"2"},
{"id":"15","name":"javascript","parent_id":"2"},
{"id":"17","name":"Computer Operator","parent_id":"1"},
{"id":"2","name":"Programming","parent_id":"1"},
{"id":"4","name":"Networking","parent_id":"1"},
{"id":"11","name":"Hardware","parent_id":"1"},
{"id":"13","name":"Driver","parent_id":"0"},
{"id":"5","name":"Engineering","parent_id":"0"},
{"id":"19","name":"Economics","parent_id":"0"},
{"id":"1","name":"Information Technology","parent_id":"0"}];
*/
$category_options = $this->multilevel_select($category);
function multilevel_select($array,$parent_id = 0,$parents = array()) {
static $i=0;
if($parent_id==0)
{
foreach ($array as $element) {
if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
$parents[] = $element['parent_id'];
}
}
}
$menu_html = '';
foreach($array as $element){
if($element['parent_id']==$parent_id){
$menu_html .= '<option>';
for($j=0; $j<$i; $j++) {
$menu_html .= '—';
}
$menu_html .= $element['name'].'</option>';
if(in_array($element['id'], $parents)){
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents);
}
}
}
$i--;
return $menu_html;
}
echo $category_options;
限制递归涉及三个步骤:
- 正在初始化作为参数传递给递归函数的计数器变量。
- 在递归之前根据所需边界测试计数器值
- 在递归调用的情况下传递递增的计数器值。
在您的情况下,这将是下面的 $level
变量:
function multilevel_select($array,$parent_id = 0,$parents = array(), $level=0) {
// ....
if(in_array($element['id'], $parents) && $level < 2){ // your boundary here, 2 for third nesting level from root
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents, $level+1);
}
}
}
$i--;
return $menu_html;
}
我想在 codeigniter 中为 select 父类别或其任何子类别生成一个多级分层 select 选项。我想将层次结构级别限制为 3。即该选项必须显示父级、其第一级子级和第二级子级,子类别缩进到父级的右侧。我没有问题遍历所有层次结构级别。但是我无法生成特定层次结构级别的 select 选项。在我的案例中,我需要三个级别的层次结构。谁能帮帮我。
这是我到目前为止尝试过的方法:
<?php
$category = $this->get_category();
function get_category() {
$this->db->order_by('parent_id', 'DESC');
$this->db->select('id, name, parent_id');
return $this->db->get('tbl_job_category')->result_array();
}
/*
*$category contains the following json data obtained from DB
$category = [{"id":"20","name":"MVC","parent_id":"16"},
{"id":"16","name":"Spring","parent_id":"14"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"6","name":"Electrical","parent_id":"5"},
{"id":"3","name":"Civil","parent_id":"5"},
{"id":"14","name":"java","parent_id":"2"},
{"id":"15","name":"javascript","parent_id":"2"},
{"id":"17","name":"Computer Operator","parent_id":"1"},
{"id":"2","name":"Programming","parent_id":"1"},
{"id":"4","name":"Networking","parent_id":"1"},
{"id":"11","name":"Hardware","parent_id":"1"},
{"id":"13","name":"Driver","parent_id":"0"},
{"id":"5","name":"Engineering","parent_id":"0"},
{"id":"19","name":"Economics","parent_id":"0"},
{"id":"1","name":"Information Technology","parent_id":"0"}];
*/
$category_options = $this->multilevel_select($category);
function multilevel_select($array,$parent_id = 0,$parents = array()) {
static $i=0;
if($parent_id==0)
{
foreach ($array as $element) {
if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
$parents[] = $element['parent_id'];
}
}
}
$menu_html = '';
foreach($array as $element){
if($element['parent_id']==$parent_id){
$menu_html .= '<option>';
for($j=0; $j<$i; $j++) {
$menu_html .= '—';
}
$menu_html .= $element['name'].'</option>';
if(in_array($element['id'], $parents)){
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents);
}
}
}
$i--;
return $menu_html;
}
echo $category_options;
限制递归涉及三个步骤:
- 正在初始化作为参数传递给递归函数的计数器变量。
- 在递归之前根据所需边界测试计数器值
- 在递归调用的情况下传递递增的计数器值。
在您的情况下,这将是下面的 $level
变量:
function multilevel_select($array,$parent_id = 0,$parents = array(), $level=0) {
// ....
if(in_array($element['id'], $parents) && $level < 2){ // your boundary here, 2 for third nesting level from root
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents, $level+1);
}
}
}
$i--;
return $menu_html;
}