谁有建公司部门的树形结构的php的源代码?要可以建三级部门
1个回答
展开全部
存储结构很简单,主要是排序后显示。
给你示例代码。
<?PHP
/*
CREATE TABLE `category` (
`categoryID` int(10) unsigned NOT NULL auto_increment,
`categoryParentID` int(10) unsigned NOT NULL default '0',
`categoryName` varchar(50) NOT NULL default '',
KEY `cate_id` (`categoryID`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
#
# 导出表中的数据 `category`
#
INSERT INTO `category` S (1, 0, '一级类别1');
INSERT INTO `category` S (2, 1, '二级类别1');
INSERT INTO `category` S (3, 1, '二级类别2');
INSERT INTO `category` S (4, 1, '二级类别3');
INSERT INTO `category` S (5, 2, '三级类别21');
INSERT INTO `category` S (6, 2, '三级类别22');
INSERT INTO `category` S (7, 2, '三级类别23');
INSERT INTO `category` S (8, 3, 'rfwesdfsd');
INSERT INTO `category` S (9, 4, '54534w43');
INSERT INTO `category` S (10, 5, '66666');
*/
mysql_connect( "localhost", 'root', '' );
mysql_select_db( "test" );
$cate_table = "category";
_GetCategory( $category_id = 0, $depth = 1 )
{
global $cate_table;
$sql = "SELECT * FROM $cate_table ORDER BY categoryID DESC";
$result = mysql_query( $sql );
while ( $row = mysql_fetch_array( $result ) )
{
$array[$row[categoryParentID]][$row[categoryID]]
= array(
'id' => $row[categoryID],
'parent' => $row[categoryParentID],
'name' => $row[categoryName]
);
}
if ( !isset( $array[$category_id] ) )
{
return "";
}
foreach( $array[$category_id] AS $key => $category )
{
echo " <OPTION =".$category['id']." ";
if ( $category['parent'] == 0 )
{
echo " class='main' ";
}
if ( $depth > 1 )
{
echo ">" . str_repeat( "--", $depth - 1 ) . " " . $category['name'] . "</option>n";
}
else
{
echo ">" . $category['name'] . "</option>\n";
}
_GetCategory( $key, $depth + 1 );
}
unset( $array[$category_id] );
}
?>
<select name="categoryID">
<option selected ="">-------------</option>
<?=_GetCategory();?>
</select>
给你示例代码。
<?PHP
/*
CREATE TABLE `category` (
`categoryID` int(10) unsigned NOT NULL auto_increment,
`categoryParentID` int(10) unsigned NOT NULL default '0',
`categoryName` varchar(50) NOT NULL default '',
KEY `cate_id` (`categoryID`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
#
# 导出表中的数据 `category`
#
INSERT INTO `category` S (1, 0, '一级类别1');
INSERT INTO `category` S (2, 1, '二级类别1');
INSERT INTO `category` S (3, 1, '二级类别2');
INSERT INTO `category` S (4, 1, '二级类别3');
INSERT INTO `category` S (5, 2, '三级类别21');
INSERT INTO `category` S (6, 2, '三级类别22');
INSERT INTO `category` S (7, 2, '三级类别23');
INSERT INTO `category` S (8, 3, 'rfwesdfsd');
INSERT INTO `category` S (9, 4, '54534w43');
INSERT INTO `category` S (10, 5, '66666');
*/
mysql_connect( "localhost", 'root', '' );
mysql_select_db( "test" );
$cate_table = "category";
_GetCategory( $category_id = 0, $depth = 1 )
{
global $cate_table;
$sql = "SELECT * FROM $cate_table ORDER BY categoryID DESC";
$result = mysql_query( $sql );
while ( $row = mysql_fetch_array( $result ) )
{
$array[$row[categoryParentID]][$row[categoryID]]
= array(
'id' => $row[categoryID],
'parent' => $row[categoryParentID],
'name' => $row[categoryName]
);
}
if ( !isset( $array[$category_id] ) )
{
return "";
}
foreach( $array[$category_id] AS $key => $category )
{
echo " <OPTION =".$category['id']." ";
if ( $category['parent'] == 0 )
{
echo " class='main' ";
}
if ( $depth > 1 )
{
echo ">" . str_repeat( "--", $depth - 1 ) . " " . $category['name'] . "</option>n";
}
else
{
echo ">" . $category['name'] . "</option>\n";
}
_GetCategory( $key, $depth + 1 );
}
unset( $array[$category_id] );
}
?>
<select name="categoryID">
<option selected ="">-------------</option>
<?=_GetCategory();?>
</select>
深圳联雅网络科技有限公司
2020-02-18 广告
2020-02-18 广告
一、申请域名,二、申请空间,三、定位网站,四、分析网站功能和需求(网站策划)。五、网站风格设计,六、网站代码制作,七、测试网站,八、FTP上传网站,九、完善资料,十、网站推广维护,...
点击进入详情页
本回答由深圳联雅网络科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询