首页  >  文章  >  后端开发  >  php无限归类

php无限归类

WBOY
WBOY原创
2016-06-13 13:16:52818浏览

php无限分类
db设计:

CREATE TABLE IF NOT EXISTS `test` (
	`id` INT(1) NOT NULL AUTO_INCREMENT,
	`path` VARCHAR(255) NOT NULL,
	`name` VARCHAR(255) NOT NULL,
	PRIMARY KEY (`id`))ENGINE = MYISAM DEFAULT CHAR SET ='utf8';
	
INSERT INTO `test` VALUES 
	('', '0', '公司'),
	('', '0-1', '部门一'),
	('', '0-2', '部门二'),
	('', '0-3', '部门三'),
	('', '0-1-1', '小组一'),
	('', '0-1-2', '小组二'),
	('', '0-2-1', '小组三'),
	('', '0-2-2', '小组四');


php代码实现:
<?php
$db = mysql_connect('localhost', 'root', 'zhw') or die('could not conenct to db');
mysql_query("SET charset 'utf8'");
mysql_select_db('test', $db);
$query = mysql_query("SELECT `id`, concat(`path`, '-', id) AS pwd, `name` FROM `test` ORDER BY `pwd`");
while ($row = mysql_fetch_array($query))
{
	$indent = str_repeat('    ', count(explode('-', $row['pwd'])) - 2);
	$option .= "<option value='{$row['id']}'>{$indent}{$row['name']}</option>";
}
echo "<select>{$option}</select>";


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn