class k_model_menu_menu
{
private $data = array();
private $rdata = array();
private $jdata = array();
private $level = 0;
private $paret = array();
function getOption($type= 'part',$pid = 0)
{
if($type = 'all') $this->data= R::getAll( 'select * from menu' );
$this->teamData();
if($this->rdata) return $this->rdata;
return false;
}
function addMenu($data){
foreach($data as $key =>$value){
if($value == '请填写内容!') $data[$key]='';
}
if($data){
$menu = R::dispense('menu');
$menu->pid = $data['pid'];
$menu->name = $data['name'];
$menu->url = $data['url'];
$menu->icon = $data['icon'];
$id = R::store($menu);
return $id;
}
}
//返回json 字符串
public function getJsonMenu(){
$data = $this->getChild(1);
$this->jdata = $data;
$this->recursive($this->jdata);
return json_encode($this->jdata);
}
//递归函数 实现不断的生成子节点,用了引用,感觉这引用是如来神笔,要不然实现太复杂了
public function recursive(&$data = array()){
foreach($data as $key =>$value){
$data[$key]['children']= $this->getChild($value['id']);
$tmp = &$data[$key]['children'];
if($tmp){
$this->recursive($tmp);
}
}
}
//组织数据,用于生成树形的select 返回的是一个数组
//数组的形式是
public function teamData($pid=1){
foreach ($this->data as $key => $value) {
if($value['pid']==$pid){
$this->level++;
array_push($this->rdata, array('name'=>$value['name'],'level'=>$this->level,'id'=>$value['id']));
$tmpdata = $this->teamData($value['id']);
if(!$tmpdata){
$this->level--;
continue;
}
}
}
}
//根据pid拿取下面的子数据
public function getChild($pid){
$data= R::getAll( "select * from menu where pid = {$pid}" );
$tmpdata = array();
if($data){
foreach ($data as $key => $value) {
$tmpdata[$key]['id'] = $value['id'];
$tmpdata[$key]['icon'] = $value['icon'];
$tmpdata[$key]['text'] = $value['name'];
$tmpdata[$key]['url'] = $value['url'];
$tmpdata[$key]['children'] = array();
}
}
return $tmpdata;
}
}
http://www.bkjia.com/PHPjc/477141.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477141.htmlTechArticle?php class k_model_menu_menu { private $data = array(); private $rdata = array(); private $jdata = array(); private $level = 0; private $paret = array(); function getOption($type= p...