php – 如何使用递归函数嵌套对注释的回复
发布时间:2020-12-13 16:59:28 所属栏目:PHP教程 来源:网络整理
导读:所以,我在评论表中有一个“父”列,我将这样的评论称为…… SELECT id,parent_id,member,name,email,ip,comment,img,date FROM cs_comments WHERE (row_id='$cmtid' AND page_type='$cmtt') ORDER BY date 我这样称呼函数…… ` $comment_data = array();whil
所以,我在评论表中有一个“父”列,我将这样的评论称为……
SELECT id,parent_id,member,name,email,ip,comment,img,date FROM cs_comments WHERE (row_id='$cmtid' AND page_type='$cmtt') ORDER BY date 我这样称呼函数…… $comment_data = array(); while ($comments_array = $comments->fetch_object()) { $comment_data[] = $comments_array; } echoComments($comment_data,$memberlevel); 我有一个功能设置来打印评论但是,如何打印回复?功能在它的内部. 如果我的问题不明确,我可以添加更多信息. 解决方法
既然你的问题很抽象,那么这就是我开始的解决方案:
// call it somewhere else inside some other function $this->recursiveCaller(); // now the function itself function recursiveCaller($parent = 0,$indent = 0) { // $Database->select is just pseudo for however you do the query!!! $Database->select('SELECT id,date FROM cs_comments WHERE (row_id='.$cmtid.' AND page_type='.$cmtt.' AND parent_id = '.$parent.') ORDER BY date'); $menuItems = $this->Database->fetchAll(); while($comments_array = $comments->fetch_object()) { ?> <div style="margin-left: <?= $indent ?>px;"> <!-- INSERT CONTENT HERE --> </div> <?php // $this assumes you are doing this inside a class // it also assumes $comment_array holds an object retrieved by the query,where $comments_array->parent_id points to the relevant parent_id column $this->recursiveCaller($comments_array->parent_id,$indent+50); } } 它将首先打印没有缩进的“基础”图层,并且每次向下递归时都会创建一个向左边距添加50px的“梯形图”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |