当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP简单实现无限级分类的方法

PHP简单实现无限级分类的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

平板电脑怎么打电话,青岛房屋出售,异界之九龙传说

本文实例讲述了php简单实现无限级分类的方法。分享给大家供大家参考,具体如下:

数据库结构:

create table if not exists `city` (
 `id` int(11) not null auto_increment,
 `name` varchar(30) character set utf8 collate utf8_unicode_ci not null default '0',
 `parentid` int(11) not null default '0'
 primary key (`id`)
) engine=myisam default charset=latin1 auto_increment=7 ;

php文件:

$db=new db($config['host'],$config['user'],$config['password'],$config['port'],$config['db'],$config['charset']);
function findcity($table,$id=0,$level=1){
  global $db;
  $findsql="select id,name,parentid from $table where parentid={$id} order by id";
  $findresult=$db->getarray($findsql);
  $num=$db->numrows;
  $logostr="|";
  for($i=0;$i<$level;$i++){
  $logostr.="--";
  }
   if($num!=0){
   for($j=0;$j<$num;$j++){
     echo "<option value={$findresult[$j]['id']}>{$logostr}{$findresult[$j][name]}</option>";
        findcity($table,$findresult[$j]['id'],$level+1);
    }
  }
}
findcity(city);

更多关于php相关内容感兴趣的读者可查看本站专题:《php数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网