PHP无限分类

2013-11-10 01:54:01 0  category: 第三阶段php

PHP无限分类


   只使用一条SQL的PHP无限分类的实现.

   


CREATE TABLE IF NOT EXISTS `xlxz_cat_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fid` int(11) NOT NULL,
`cat_name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;

INSERT INTO `xlxz_cat_name` (`id`, `fid`, `cat_name`) VALUES
(1, 0, 'xlxz'),
(2, 0, 'no2'),
(3, 1, 'php mysql'),
(4, 1, 'c freebsd'),
(5, 1, 'linux ubuntu'),
(7, 1, 'python'),
(8, 1, 'unix c++'),
(9, 2, 'perl'),
(10, 2, 'shell'),
(11, 2, 'csh'),
(12, 2, 'bash'),
(13, 5, 'tsh'),
(14, 5, 'centos'),
(15, 5, 'iptables'),
(16, 5, 'postgresql'),
(17, 1, 'php freamwork'),
(18, 12, 'codeigniter'),
(19, 13, 'yii'),
(20, 12, 'thinkphp'),
(21, 13, 'pc-bsd'),
(22, 2, 'java'),
(23, 2, 'perl python c++/c java clisp');


   

<?php
$link = mysql_connect( '127.0.0.1', 'root', '123456' ) or die( mysql_error() );//连接数据库服务器
mysql_select_db( 'xlxz' );//选择数据库
mysql_set_charset( 'utf8' );//设置字符编码

$sql = "SELECT * FROM xlxz_cate_name";//查询分类表

$result = mysql_query( $sql );
$xlxz = array();//初始化一个数组
while( $row = mysql_fetch_assoc( $result ) )
{
$xlxz[$row['fid']][$row['id']] = $row['cat_name'];//创建分类数组
}
unset( $row );

mysql_close( $link );//关闭数据库



var_dump( $xlxz );//打印出分类数组

echo '-------------------------------------------' . "\n";


//递归函数

function for_category( $ss = array(), $a2 )
{
echo "<ul>";
foreach ( $ss as $k => $v )
{
if ( isset( $a2[$k] ) && is_array( $a2[$k] ) )
{ echo "<li>$k $v";
for_category( $a2[$k], $a2 );
echo "</li>";
}
else{
echo "<li>$k $v</li>";
}
}
echo "</ul>";
}



for_category( $xlxz[0], $xlxz );




<?php
/**
* $tree= new xlxzTree($result);
* $arr=$tree->leaf(0);
* $nav=$tree->navi(15);
*/
class xlxzTree {
private $result;
private $tmp;
private $arr;
private $already = array();
/**
* 构造函数
*
* @param array $result 树型数据表结果集
* @param array $fields 树型数据表字段,array(分类id,父id)
* @param integer $root 顶级分类的父id
*/
public function __construct($result, $fields = array('id', 'fid'), $root = 0) {
$this->result = $result;
$this->fields = $fields;
$this->root = $root;
$this->handler();
}
/**
* 树型数据表结果集处理
*/
private function handler() {
$tmp = array();
foreach ($this->result as $node) {
$tmp[$node[$this->fields[1]]][] = $node;
}
krsort($tmp);
for ($i = count($tmp); $i > 0; $i--) {
foreach ($tmp as $k => $v) {
if (!in_array($k, $this->already)) {
if (!$this->tmp) {
$this->tmp = array($k, $v);
$this->already[] = $k;
continue;
} else {
foreach ($v as $key => $value) {
if ($value[$this->fields[0]] == $this->tmp[0]) {
$tmp[$k][$key]['child'] = $this->tmp[1];
$this->tmp = array($k, $tmp[$k]);
}
}
}
}
}
$this->tmp = null;
}
$this->tmp = $tmp;
}
/**
* 反向递归
*/
private function recur_n($arr, $id) {
foreach ($arr as $v) {
if ($v[$this->fields[0]] == $id) {
$this->arr[] = $v;
if ($v[$this->fields[1]] != $this->root) $this->recur_n($arr, $v[$this->fields[1]]);
}
}
}
/**
* 正向递归
*/
private function recur_p($arr) {
foreach ($arr as $v) {
$this->arr[] = $v[$this->fields[0]];
if ($v['child']) $this->recur_p($v['child']);
}
}
/**
* 菜单 多维数组
*
* @param integer $id 分类id
* @return array 返回分支,默认返回整个树
*/
public function leaf($id = null) {
$id = ($id == null) ? $this->root : $id;
return $this->tmp[$id];
}
/**
* 导航 一维数组
*
* @param integer $id 分类id
* @return array 返回单线分类直到顶级分类
*/
public function navi($id) {
$this->arr = null;
$this->recur_n($this->result, $id);
krsort($this->arr);
return $this->arr;
}
/**
* 散落 一维数组
*
* @param integer $id 分类id
* @return array 返回leaf下所有分类id
*/
public function leafid($id) {
$this->arr = null;
$this->arr[] = $id;
$this->recur_p($this->leaf($id));
return $this->arr;
}
}



$pdo = new pdo( 'mysql:host=192.168.56.224;dbname=qqsq;charset=utf8', 'root', '123456' );
$sql = "SELECT * FROM qqsq_cat_id_name";


$arr = $pdo->query( $sql )->fetchAll( PDO::FETCH_ASSOC );

$tree = new xlxztree( $arr );

$arr = $tree->leaf(0);

$nav = $tree->navi( 10 );


var_dump( $arr, $nav );

foreach ( $nav as $k => $v )
{
var_dump( $k, $v );
}