顶级奉献 - 可视化无限极菜单或结构创建(原创100%)

目前互联网充斥着各式各样的创建菜单的功能,有些是限制级菜单,有些是需要繁琐的填写排列序号等,或有些没能可视化地清晰表现出整个菜单的布局或外观,2011年科技日新月异的时代,难道我们怎么还能被这些困扰着落后着!就让我们开始吧。。。
正好公司在开发项目的之际,为了能给使用者提供更方便的操作,所以开发了一个可视化无限极菜单,由于本项目是基于CodeIgniter 1.7 的MVC的框架开发,并项目属于公司内部机密,没有案例展示给大家看,仅此把几个递归算法和大家分享,另外附上 菜单使用到的 css/html/Javascritp.

//======用于构建菜单的三个递归算法函数===CopyRight Terry 2011-05-25============================================
//创建多维菜单数组
function _CreateMenuArray($rdarray = array(),$keynum = 0)
{
$newarray = array();
if(is_array($rdarray))
{
foreach($rdarray as $key => $value)
{
$num = substr_count($value['level'],'-')-2;
if($num!=0)
{
$newarray_temp = $this->_SearchArray($newarray,$value);
if($this->_arraydif == 1)
{
$newarray = $newarray_temp;
}
else
{
//********额外********
if($value['type']==0)
{
$mtype = $this->lang->line('article_type_type_list');
}
else
{
$mtype = $this->lang->line('article_type_type_page');
}
if($value['tomenu']==0)
{
$minm = $this->lang->line('noinf');
}
else
{
$minm = $this->lang->line('haveinf');
}
//*************************
$newarray[$keynum] = array(
'id'     => $value['id'],
'name'     => $value['name'],
'level'     => $value['level'],
'type'     => $mtype,
'tomenu' => $minm
);
$keynum+=1;
}
}
else
{
//********额外********
if($value['type']==0)
{
$mtype = $this->lang->line('article_type_type_list');
}
else
{
$mtype = $this->lang->line('article_type_type_page');
}
if($value['tomenu']==0)
{
$minm = $this->lang->line('noinf');
}
else
{
$minm = $this->lang->line('haveinf');
}
//*************************
$newarray[$keynum] = array(
'id'     => $value['id'],
'name'     => $value['name'],
'level'     => $value['level'],
'type'     => $mtype,
'tomenu' => $minm
);
$keynum+=1;
}
$this->_arraydif = 0;
}
}
return $newarray;
}

//遍历已生成的菜单数组
function _SearchArray($sarray = array(),$addarray = array())
{
if(is_array($sarray))
{
$find = 0;
foreach($sarray as $skey => $svalue)
{
if(is_array($svalue))
{
if(
@stripos($addarray['level'],$svalue['level'])===0
&&
substr_count($addarray['level'],'-')===(substr_count($svalue['level'],'-')+1)
)
{
//********额外********
if($addarray['type']==0)
{
$mtype = $this->lang->line('article_type_type_list');
}
else
{
$mtype = $this->lang->line('article_type_type_page');
}
if($addarray['tomenu']==0)
{
$minm = $this->lang->line('noinf');
}
else
{
$minm = $this->lang->line('haveinf');
}
$addarray['type'] = $mtype;
$addarray['tomenu'] = $minm;
//*************************

$svalue['children'][]= $addarray;
$sarray[$skey] = $svalue;
$find = 1;
$this->_arraydif = $find;
}
if($find==0)
{
if(is_array($svalue))
$sarray[$skey] = $this->_SearchArray($svalue,$addarray);
}
}
}
}
return $sarray;
}
//生成HTML菜单结构
function _CreateMenuHTML($menuarray = array(),$menustr = '')
{
if(is_array($menuarray))
{
foreach($menuarray as $key => $value)
{
if(is_array($value) && count($value)==6)
{
$menustr.='
<div id="'.$value['id'].'">
<div>'.$value['name'].'<ul><li>'.$value['tomenu'].'</li><li>'.$value['type'].'</li><li><a href="'.site_url('adminsystem/settings/sys_articletype_edit/').'/'.$value['id'].'">'.$this->lang->line('publick_btn_edit').'</a></li><li><a href="javascript:void(0);" onclick="confirmdialog(2,'.$value['id'].')">'.$this->lang->line('publick_btn_del').'</a></li></ul></div>
';
$menustr.= $this->_CreateMenuHTML($value['children']).
'</div>';
}
else
{
$menustr.='
<div id="'.$value['id'].'">
<div>'.$value['name'].'<ul><li>'.$value['tomenu'].'</li><li>'.$value['type'].'</li><li><a href="'.site_url('adminsystem/settings/sys_articletype_edit/').'/'.$value['id'].'">'.$this->lang->line('publick_btn_edit').'</a></li><li><a href="javascript:void(0);" onclick="confirmdialog(2,'.$value['id'].')">'.$this->lang->line('publick_btn_del').'</a></li></ul></div>
</div>';
}
}
$menustr = '
<span>
'.$menustr.'
</span>
';
}
else
{
$menustr = '';
}
return $menustr;
}
//===========================================================================================
 
//保存文章类型的菜单结构
function sys_articletype_savemenu()
{
if($_POST['menustr']!='')
{
$menulist = array();
$menu = explode('##',$_POST['menustr']);
$fid = 0;
$levelnum = 0;
$levelstr = '';
foreach($menu as $key=>$val)
{
$id = substr($val,stripos($val,'=')+1,strlen($val));
$level = substr_count($val,'children');
if($level>$levelnum)
{
$menulist[$key]=array(
'id'     => $id,
'level' => $levelstr.$key.'-'
);
$levelnum = $level;
$levelstr = $levelstr.$key.'-';
}
elseif($levelnum!=0 && $level==$levelnum)
{
$leveltemp = substr($levelstr,1,strlen($levelstr)-2);
$leveltemp = '-'.substr($leveltemp,0,strripos($leveltemp,'-')+1);
$menulist[$key]=array(
'id'     => $id,
'level' => $leveltemp.$key.'-'
);
$levelnum = $level;
$levelstr = $leveltemp.$key.'-';
}
else
{
$levelnum = $level;
if($level == 0)
{
$fid = $key;
}
$levelstr = '';
for($i=$fid;$i<$levelnum+$fid;$i++)
{
$levelstr.='-'.$i;
}
$levelstr.='-'.$key.'-';
$menulist[$key]=array(
'id'     => $id,
'level' => $levelstr
);
}
}
//print_r($menulist);exit;
//SQL事务处理
mysql_query('START TRANSACTION') or exit(mysql_error());
foreach($menulist as $key => $val)
{
mysql_query(
'UPDATE '.
$this->session->userdata('DP').'articles_type
SET
level = "'.$val['level'].'",
sortid = '.$key.'
WHERE id='.$val['id']);
}
mysql_query('COMMIT') or exit(mysql_error());//执行事务
}
}

Need a translation service?

Please enter your personal details and we will contact you shortly

Words translated by CCJK

146,096,379

We are Certified

Our Client Satisfaction

rating for previous quarte

4.00

Over 95% of our clients recommend our language services to others

Copyright © CCJK Technologies Co., Ltd. 2000-2017. All rights reserved.
TOP