Home > php教程 > php手册 > body text

php 实现无限级树型示例演示

WBOY
Release: 2016-05-26 08:20:47
Original
1095 people have browsed it

无限级分类功能就是用到无限级树了,在此我们一起来看看小编找到一个非常不错并且简单的php 实现无限级树型示例供各位学习参考。


php 实现无限级树型可以实现再主菜单里面有哪些二级才能。也可以开发留言板的时候获取父评论下的子评论!这个就是实现无限级树型的功能了!那么我们话就不多说了。请看看下面数据库的结构

CREATE TABLE `emlog_hudhf` (
  `id` mediumint(10) unsigned NOT NULL AUTO_INCREMENT,
  `sslog` varchar(64) NOT NULL,
  `sid` varchar(64) NOT NULL,
  `s` varchar(64) NOT NULL,
  `z` varchar(64) NOT NULL,
  `n` text NOT NULL,
  `puser` varchar(64) NOT NULL,
  `buser` varchar(64) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=128 DEFAULT CHARSET=utf8 AUTO_INCREMENT=128 ;
-- 
-- 导出表中的数据 `emlog_hudhf`
--
INSERT INTO `emlog_hudhf` VALUES (46, '5', '0', '1397957923', '0', '评论了说说啦!', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (82, '13', '0', '1398523128', '0', '什么和什么', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (48, '6', '0', '1397958405', '0', '你看评论啊', '257', '257');
INSERT INTO `emlog_hudhf` VALUES (70, '8', '69', '1397983195', '0', '我又回复你了', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (117, '8', '66', '1398561020', '0', '我也回复你', '1', '497');
INSERT INTO `emlog_hudhf` VALUES (66, '8', '0', '1397982730', '0', '我要评论你XXXXXXXXHappySky', '497', '1');
INSERT INTO `emlog_hudhf` VALUES (68, '7', '0', '1397982850', '0', '测试评论功能 可以恢复不', '1', '497');
INSERT INTO `emlog_hudhf` VALUES (69, '8', '65', '1397982864', '0', '回复“什么呀”~~~~', '497', '1');
INSERT INTO `emlog_hudhf` VALUES (120, '8', '0', '1398561155', '0', '我也评论一下自己的说说', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (121, '8', '0', '1398561255', '0', '说说', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (124, '8', '120', '1398561490', '0', '很好吧', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (123, '8', '66', '1398561328', '0', '去你妈妈个B', '1', '497');
Copy after login


这个是数据库的图片结构

QQ图片20140427155330.jpg
然后思路是这样的:sid为0的表示这个是父评论不是0就是子评论了!然后怎么知道那个子评论是属于那个父评论的呢?再sid不为0的话那么就根据这个sid的值去查询等于这个值得id看看下面的列子

INSERT INTO `emlog_hudhf` VALUES (120, '8', '0', '1398561155', '0', '我也评论一下自己的说说', '1', '1');
INSERT INTO `emlog_hudhf` VALUES (124, '8', '120', '1398561490', '0', '很好吧', '1', '1');
Copy after login


第二条的sid等于第一条的id 那么 这个二条的就是第一条的子评论了!

然后要怎么输出呢。当然首先要获取你要输出数据的sql语句

//这一段是意思呢。sslog 等于 这个变量whidi的值得数据 然后按最新插入来显示出来
$gg = mysql_query("SELECT * FROM ".DB_PREFIX."hudhf WHERE sslog = '".$whidi."' order by id desc");
$data = array();
while($rs = mysql_fetch_assoc($gg)){  
          $data[] = $rs;//这个是把他转换成为数组后给另一个函数来输出 
}
getTree($data, 0,$whidi,$whiuser);
}
echo getTree($data, 0); 
function getTree($data, $pId)
{
$html = '';foreach($data as $k => $v)
{
   if($v['cate_ParentId'] == $pId)
   {         //父亲找到儿子
    $html .= "
Copy after login


然后这个就是结合html美化后的结果了


QQ图片20140427160240.jpg

本文地址:

转载随意,但请附上文章地址:-)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!