Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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
PHP-递归函数中的增量_Php_Codeigniter - Fatal编程技术网

PHP-递归函数中的增量

PHP-递归函数中的增量,php,codeigniter,Php,Codeigniter,我有这个功能: function show_comments(&$comments, $parent_id = 0 ) { $comments_list = ""; $i = 0; foreach($comments as $comment) : if ($comment["parent_id"] != $parent_id) continue; $comments_list .= '<div class="comment level-

我有这个功能:

function show_comments(&$comments, $parent_id = 0 ) {
  $comments_list = "";
  $i = 0;
  foreach($comments as $comment) :
    if ($comment["parent_id"] != $parent_id)
        continue;

    $comments_list .= '<div class="comment level-'. $i . '">';
    $i++;
    $comments_list .= "<p>$comment[body]</p>";
    $comments_list .= $this->show_comments($comments, $comment['id_comment']);
    $comments_list .= '</div>';
  endforeach;


  return $comments_list;
}
函数显示注释(&$comments,$parent\u id=0){
$comments_list=“”;
$i=0;
foreach($comments作为$comment):
如果($comment[“parent\u id”!=$parent\u id)
继续;
$comments_list.='';
$i++;
$comments\u list.=“$comment[body]

”; $comments\u list.=$this->show\u comments($comments,$comment['id\u comment']); $comments_list.=''; endforeach; 返回$comments\u列表; }
我希望父div具有classlevel-0,该父div的直接子div具有classlevel-1,具有classlevel-1的子div具有classlevel-2,依此类推。如何执行此操作?

//添加新参数--------------------------------
// add a new parameter --------------------------------
//                                                    |
function show_comments(&$comments, $parent_id = 0, $level = 0) {
  $comments_list = "";

  // not needed
  // $i = 0;

  foreach($comments as $comment) :
    if ($comment["parent_id"] != $parent_id)
        continue;

    // use the level parameter ------------------------
    //                                                |
    $comments_list .= '<div class="comment level-'. $level . '">';

    // not needed
    // $i++;
    $comments_list .= "<p>$comment[body]</p>";


    // increment it on recursive calls -----------------------------------------
    //                                                                         |
    $comments_list .= $this->show_comments($comments, $comment['id_comment'], $level + 1);
    $comments_list .= '</div>';
  endforeach;


  return $comments_list;
}
// | 函数show_comments(&$comments,$parent_id=0,$level=0){ $comments_list=“”; //不需要 //$i=0; foreach($comments作为$comment): 如果($comment[“parent\u id”!=$parent\u id) 继续; //使用级别参数------------------------ // | $comments_list.=''; //不需要 //$i++; $comments\u list.=“$comment[body]

”; //在递归调用时递增它----------------------------------------- // | $comments\u list.=$this->show\u comments($comments,$comment['id\u comment'],$level+1); $comments_list.=''; endforeach; 返回$comments\u列表; }
//添加新参数--------------------------------
//                                                    |
函数show_comments(&$comments,$parent_id=0,$level=0){
$comments_list=“”;
//不需要
//$i=0;
foreach($comments作为$comment):
如果($comment[“parent\u id”!=$parent\u id)
继续;
//使用级别参数------------------------
//                                                |
$comments_list.='';
//不需要
//$i++;
$comments\u list.=“$comment[body]

”; //在递归调用时递增它----------------------------------------- // | $comments\u list.=$this->show\u comments($comments,$comment['id\u comment'],$level+1); $comments_list.=''; endforeach; 返回$comments\u列表; }
您必须循环浏览所有注释以构建树。或者您可以跟踪数据库中的级别id。您必须循环所有注释以构建树。或者您可以跟踪数据库中的级别id。