加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

php无穷级评述嵌套实当代码

发布时间:2021-05-24 07:38:51 所属栏目:编程 来源:网络整理
导读:我在计划BB的进程中,也一向在思索是否可以不通过递回来实现无穷级分类的布局揭示和父子布局查找,由于假如差池这里的算法举办优化效果也许是致命的!试想一下,一篇文章假如评述数为300,按正常的递归算法,至少就得查询数据库301次,并且照旧在没有任何嵌

我在计划BB的进程中,也一向在思索是否可以不通过递回来实现无穷级分类的布局揭示和父子布局查找,由于假如差池这里的算法举办优化效果也许是致命的!试想一下,一篇文章假如评述数为300,按正常的递归算法,至少就得查询数据库301次,并且照旧在没有任何嵌套的环境下,假若有过一两级嵌套可能评述数过1000,那数据库不是直接宕掉? 而现实上,PHP强盛的数组处理赏罚手段已经能辅佐我们快速利便的办理这个题目。下图为一个无穷级分类的

数据库布局:

IDparentID newsID commts 108文章ID为8的评述 21 8对ID为1的评述的回覆 328对ID为2的评述的回覆

要在前台嵌套式的揭示文章编号8的评述,着实我们只用查询一次数据库,即“SELECT * FROM TABLE WHERE newsID=8”,而把后期的递归事变交给强盛的PHP数组来完成。这里也许涉及的题目就是数组的布局相关的重组,即将全部逗留在一级分类上的评述所有放到本身的parentID下,形成children项。 下面将BBComment类中这块的代码粘贴出来,但愿与各人分享下我的思绪,也但愿各人可以或许提出更好更有服从的算法。

要领一

$value) { if ( $value['id'] == $id ) return $value; if ( isset($value['children']) && is_array($children) ) $this->getCommentsFormAryById($value['children'],$id); } } /** * 追加 子评述 到 主评述 中,并形成children子项 * * @param array $commtAry 原评述数据引用 * @param int $parentId 主评述ID * @param array $childrenAry 子评述的值 */ function addChildenToCommentsAry($commtAry,$parentId,$childrenAry) { if ( !is_array($commtAry) ) return FALSE;

foreach($commtAry as $key=>$value) {
if ( $value['id'] == $parentId ) {
$commtAry[$key]['children'][] = $childrenAry;
return TRUE;
}
if ( isset($value['children']) ) $this->addChildenToCommentsAry($commtAry[$key]['children'],$childrenAry);
}
}
$result = $this->BBDM->select($table,$column,$condition,1000);

/ 开始举办嵌套评述布局重组 /
array_shift($result);
$count = count($result);
$i = 0;
while( $i<$count ) {
if ( '0' != $result[$i]['parentId'] ) {
$this->addChildenToCommentsAry($result,$result[$i]['parentId'],$result[$i]);
unset($result[$i]);
}
$i++;
}
$result = array_values($result);
/ 重组竣事 /

实现要领二

焦点代码摘自WordPress

'3','parent' => '0' ),array ( 'id' => '9',array ( 'id' => '1','parent' => '3' ),array ( 'id' => '2',array ( 'id' => '5','parent' => '1' ),array ( 'id' => '7','parent' => '1' ) ); function html5_comment($comment) { echo '
  • '; echo 'id:',$comment['id'],' parent:',$comment['parent']; } function start_el(& $output,$comment) { ob_start(); html5_comment($comment); $output .= ob_get_clean(); } function end_el(& $output) { $output .= "
  • n"; } function start_lvl(& $output) { $output .= '
      $depth +1 && isset ($children_elements[$id])) { //假如没高出最大层,而且存在子元素数组 foreach ($children_elements[$id] as $child) { if (!isset ($newlevel)) { //第一次轮回没配置变量$newlevel,以是把$newlevel设为true,而且开始子元素的开始代码;第二次及之后的轮回,已经配置了$newlevel,就不会再添加子元素的开始代码。由于统一批轮回时兄弟元素,以是只必要一个子元素开始代码,轮回内容为并列相关。 $newlevel = true; start_lvl($output); } display_element_template($child,$children_elements,$depth +1,$output); //$child作为参数,继承去探求下级元素 } unset ($children_elements[$id]); //用完开释变量,往后就不会一再判定该值了,递归后继承判定剩下的子元素 } if (isset ($newlevel) && $newlevel) { //假如前面找到了子元素,这里就要执行子元素的竣事代码 end_lvl($output); } end_el($output); //当前评述的竣事代码 } function display_element_template($e,& $output) { $id = $e['id']; display_element($e,$output); if ($max_depth <= $depth +1 && isset ($children_elements[$id])) { //假如超出最大层级,而且子元素存在的话,以$child为参数继承往下找 foreach ($children_elements[$id] as $child) { display_element_template($child,$output); } unset ($children_elements[$id]); //用完开释变量 } } function comments_list($comments) { $top_level_elements = array (); $children_elements = array (); foreach ($comments as $e) { if (0 == $e['parent']) { $top_level_elements[] = $e; } else { $children_elements[$e['parent']][] = $e; } } $output = ''; foreach ($top_level_elements as $e) { display_element_template($e,2,$output); } //var_dump($children_elements);//因为每次用完$children_elements后城市开释变量,以是到最后$children_elements为空数组 return $output; } echo '
        ';

        这篇文章就先容到这了,着实各人多参考一些开源的cms也可以看到许多不错的代码,但愿各人往后多多支持编程之家

        (编辑:湖南网)

        【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      热点阅读