php+mysql 如何实现多级分类啊

jackxiang 2009-2-11 18:19 | |
参看:http://www.phpe.net/articles/83.shtml
<?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` VALUES (1, 0, '一级类别1');
INSERT INTO `category` VALUES (2, 1, '二级类别1');
INSERT INTO `category` VALUES (3, 1, '二级类别2');
INSERT INTO `category` VALUES (4, 1, '二级类别3');
INSERT INTO `category` VALUES (5, 2, '三级类别21');
INSERT INTO `category` VALUES (6, 2, '三级类别22');
INSERT INTO `category` VALUES (7, 2, '三级类别23');
INSERT INTO `category` VALUES (8, 3, 'rfwesdfsd');
INSERT INTO `category` VALUES (9, 4, '54534w43');
INSERT INTO `category` VALUES (10, 5, '66666');
有问题请不要问我就行了。其他OK。欢迎修改使用。这是简单的。

*/
mysql_connect( "localhost", 'root', '' );
mysql_select_db( "test3" );
$cate_table = "category";

function _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 VALUE=".$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 value="">-------------</option>
<?=_GetCategory();?>
</select>

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1622/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]