### 一、排序
1.表单
`<input type="text" name="{$v.id}" value="{$v.sort}" />`
利用id和值进行更新
2.数据处理
~~~
foreach ($_POST as $id=>$sort){
$db->where(array('id'=>$id))->setField('sort',$sort);
}
~~~
### 二、按类递归
1. 静态类
~~~
class.Category.php
<?php
class Category{
//组合成一维数组
static public function unlimitedForLevel($cate, $html='--', $pid=0, $level=0){
$arr=array();
foreach($cate as $v){
if($v['pid']==$pid){
$v['level']=$level++;
$v['html']=str_repeat($html,$level);
$arr[]=$v;
$arr=array_merge($arr,self::unlimitedForLevel($cate, $htmml, $v['id'], $level+1));
}
}
return $arr;
}
//组合成多维数组
static public function unlimitedForLayer($cate,$name='child',$pid=0){
$arr=array();
foreach($cate as $v){
if($v['pid']==$pid){
$v[$name]=self::unlimitedForLayer($cate,$name,$v['id']);
$arr[]=$v;
}
}
return $arr;
}
//返回所有父集数组
static public function getParents($cate,$id){
$arr=array();
foreach($cate as $v){
if($v['id']==$id){
$arr[]=$v;
$arr=array_merge(self::getParents($cate,$v['pid']),$arr);
}
}
return $arr;
}
//返回所有子集id数组
static public function getChildsId($cate, $pid){
$arr=array();
foreach ($cate as $v){
if($v['pid']==$pid){
$arr[]=$v['id'];
$arr=array_merge($arr,self::getChildsId($cate,$v['id']);
}
}
return $arr;
}
//传递一个父集分类ID返回所有子级分类数组
static public function getChildsId($cate, $pid){
$arr=array();
foreach ($cate as $v){
if($v['pid']==$pid){
$arr[]=$v;
$arr=array_merge($arr,self::getChildsId($cate,$v['id']);
}
}
return $arr;
}
}
?>
~~~
2.静态调用
~~~
import('Class.Category',APP_PATH);
$cate=M('cate')->orde('sort asc')->select();
$this->cate=Category::unlimiteForLevel($cate);
$this->display();
~~~