CREATE TABLE `tree` (
`id` int(11) NOT NULL,
`name` varchar(255) collate utf8_bin NOT NULL,
`parent` int(11) NOT NULL,
`ischild` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
PHP code
mysql_connect('localhost', 'root', '123456');
mysql_select_db("wz");
mysql_query("set names 'utf8'");
//获得顶节点
$sql = "select id, name,ischild from tree where parent = 0 order by id asc";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
extract($row);
mysql_connect('localhost', 'root', '123456');
mysql_select_db("wz");
mysql_query("set names 'utf8'");
//获得顶节点
$sql = "select id, name,ischild from tree where parent = 0 order by id asc";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
extract($row);
$icon = $ischild ? "javascript:expand(\"div$id\");'>+":"-";
$name = $icon . $name;
echo "
}
function getNode($id, $level)
{
$sql = "select id, name, ischild from tree where parent = $id order by id asc ";
$result = mysql_query($sql);
$level++;
while($row = mysql_fetch_array($result))
{
extract($row);
$icon = $ischild ? "+":"-";
$name = $icon . $name;
echo "
}
}
function echoChar($char, $num)
{
for($i=0;$i< $num; $i++) { $strChar .= $char; } return $strChar; } ?>
$name = $icon . $name;
echo "
" . $name;
if($ischild)
{
getNode($id, 0);
}
echo "
";if($ischild)
{
getNode($id, 0);
}
echo "
}
function getNode($id, $level)
{
$sql = "select id, name, ischild from tree where parent = $id order by id asc ";
$result = mysql_query($sql);
$level++;
while($row = mysql_fetch_array($result))
{
extract($row);
$icon = $ischild ? "+":"-";
$name = $icon . $name;
echo "
" . echoChar(" ", $level) . $name;
if($ischild)
{
getNode($id, $level);
}
echo "
";if($ischild)
{
getNode($id, $level);
}
echo "
}
}
function echoChar($char, $num)
{
for($i=0;$i< $num; $i++) { $strChar .= $char; } return $strChar; } ?>
没有评论:
发表评论