當前位置:編程學習大全網 - 源碼下載 - PHP MYSQL 的多級分類樹結構

PHP MYSQL 的多級分類樹結構

打開數據庫那就不寫了。

前幾天剛寫了壹個。妳看下

/* 表結構

CREATE TABLE `lh_categroy` (

`id` int(10) NOT NULL auto_increment,

`parentid` int(6) NOT NULL,

`name` varchar(255) NOT NULL,

`keyword` varchar(255) NOT NULL COMMENT '關鍵字',

`des` varchar(255) NOT NULL COMMENT '描述',

PRIMARY KEY ?(`id`),

UNIQUE KEY `name` (`name`)

) ENGINE=MyISAM AUTO_INCREMENT=44 DEFAULT CHARSET=utf8;

*/

/**

* 獲得所有欄目排序後的列表

* @return array

*/

function getAllOrderColumns($id = 0) {

global $columns;

$result = array();

if ($id) $result[$id] = $columns[$id];

foreach ($columns as $column) {

if ($column['parentid'] == $id) {

$column['level'] = 0;

$result[$column['id']] = $column;

getColumns($columns, $column['id'], $result, 1);

}

}

return $result;

}

function getColumns($columns, $cid, &$result, $l = 1) {

foreach ($columns as $c) {

if ($c['parentid'] == $cid) {

$c['level'] = $l;

$result[$c['id']] = $c;

getColumns($columns, $c['id'], $result, $l + 1);

}

}

}

$sql = 'select * from lh_categroy';

$query = mysql_query($sql);

while($row = mysql_fetch_assoc($query)){

$columns[]=$row;

}

$fenlei = '';

$fenlei = ?'<select name="cid" style="width:200px; height:25px;">

<option value="0">請選擇分類</option>';

foreach(getAllOrderColumns() as $v){

$v[name] = $v[level] ? ($v[level]==1 ? '&nbsp;&nbsp;|-'.$v[name]:'&nbsp;&nbsp;&nbsp;|-'.$v[name]) : $v[name];

$fenlei .= "<option value='$v[id]'>$v[name]</option>";

}

$fenlei .= '</select>';

echo $fenlei;

  • 上一篇:股票k線中的紅綠黃白線是什麽意思?
  • 下一篇:監控系統室外設備箱的內部安裝規範
  • copyright 2024編程學習大全網