<?
require_once("echo.php");
?>
<html>
<head>
<title>生成多级分类树测试-〉搜array法</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<style>
body {
scrollbar-base-color:#517CEA;scrollbar-arrow-color:#FFFFFF;
font:14px verdana;
}
*{line-height:1;margin:0px;PADDING:0px;}
</style>
<script language="javascript">
function showHide(obj){
var oStyle = obj.parentElement.parentElement.parentElement.rows[1].style;
oStyle.display == "none" ? oStyle.display = "block" : oStyle.display = "none";
}
function showHide2(obj){
var oStyle = obj.parentElement.parentElement.div[1].style;
oStyle.display == "none" ? oStyle.display = "block" : oStyle.display = "none";
}
</script>
<base target="main">
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" target="main">
<pre>
<?php
$rowsIndex=array('66'=>'66_0','67'=>'67_0','68'=>'68_67','69'=>'69_67','70'=>'70_69','71'=>'71_0','72'=>'72_71','73'=>'73_71','74'=>'74_71','75'=>'75_0','76'=>'76_75','77'=>'77_75','78'=>'78_75','79'=>'79_75','80'=>'80_0','81'=>'81_80','82'=>'82_80','83'=>'83_80','84'=>'84_80','85'=>'85_0','86'=>'86_85','87'=>'87_0','88'=>'88_87','89'=>'89_0','90'=>'90_89','91'=>'91_89','92'=>'92_0','93'=>'93_92'
);
$rows=array('66_0'=>
array('indexKey'=>'66_0','id'=>'66','pid'=>'0','title'=>'<a href="#" target="_parent">退出DEDECMS</a>','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'67_0'=>
array('indexKey'=>'67_0','id'=>'67','pid'=>'0','title'=>'频道管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'68_67'=>
array('indexKey'=>'68_67','id'=>'68','pid'=>'67','title'=>'板块模板管理','link'=>'../web_type_web.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'69_67'=>
array('indexKey'=>'69_67','id'=>'69','pid'=>'67','title'=>'通用模板管理','link'=>'../file_view.php?activepath=<?=$mod_dir?>','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'70_69'=>
array('indexKey'=>'70_69','id'=>'70','pid'=>'69','title'=>'主页创建向导','link'=>'../add_home_page.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'71_0'=>
array('indexKey'=>'71_0','id'=>'71','pid'=>'0','title'=>'内容维护','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'72_71'=>
array('indexKey'=>'72_71','id'=>'72','pid'=>'71','title'=>'待审核文章','link'=>'../list_news_member.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'73_71'=>
array('indexKey'=>'73_71','id'=>'73','pid'=>'71','title'=>'专题管理','link'=>'../list_news_spec.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'74_71'=>
array('indexKey'=>'74_71','id'=>'74','pid'=>'71','title'=>'评论管理','link'=>'../list_feedback.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'75_0'=>
array('indexKey'=>'75_0','id'=>'75','pid'=>'0','title'=>'内容发布','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'76_75'=>
array('indexKey'=>'76_75','id'=>'76','pid'=>'75','title'=>'专题创建向导','link'=>'../add_news_spec.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'77_75'=>
array('indexKey'=>'77_75','id'=>'77','pid'=>'75','title'=>'图集发布向导','link'=>'../add_news_pic.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'78_75'=>
array('indexKey'=>'78_75','id'=>'78','pid'=>'75','title'=>'软件发布向导','link'=>'../add_news_soft.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'79_75'=>
array('indexKey'=>'79_75','id'=>'79','pid'=>'75','title'=>'Flash向导','link'=>'../add_news_flash.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'80_0'=>
array('indexKey'=>'80_0','id'=>'80','pid'=>'0','title'=>'附助功能','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'81_80'=>
array('indexKey'=>'81_80','id'=>'81','pid'=>'80','title'=>'友情链接管理','link'=>'../add_friendlink.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'82_80'=>
array('indexKey'=>'82_80','id'=>'82','pid'=>'80','title'=>'投票管理','link'=>'../add_vote.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'83_80'=>
array('indexKey'=>'83_80','id'=>'83','pid'=>'80','title'=>'论坛扩展','link'=>'../bbs_addons.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'84_80'=>
array('indexKey'=>'84_80','id'=>'84','pid'=>'80','title'=>'留言簿管理','link'=>'<?=$art_php_dir?>/guestbook/index.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'85_0'=>
array('indexKey'=>'85_0','id'=>'85','pid'=>'0','title'=>'会员管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'86_85'=>
array('indexKey'=>'86_85','id'=>'86','pid'=>'85','title'=>'网上会员管理','link'=>'../list_user.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'87_0'=>
array('indexKey'=>'87_0','id'=>'87','pid'=>'0','title'=>'文件管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'88_87'=>
array('indexKey'=>'88_87','id'=>'88','pid'=>'87','title'=>'文件浏览器','link'=>'../file_view.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'89_0'=>
array('indexKey'=>'89_0','id'=>'89','pid'=>'0','title'=>'数据库管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'90_89'=>
array('indexKey'=>'90_89','id'=>'90','pid'=>'89','title'=>'数据备份/还原','link'=>'../sys_back_data.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'91_89'=>
array('indexKey'=>'91_89','id'=>'91','pid'=>'89','title'=>'MySQL命令','link'=>'../sys_domysql.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'92_0'=>
array('indexKey'=>'92_0','id'=>'92','pid'=>'0','title'=>'系统帮助','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'93_92'=>
array('indexKey'=>'93_92','id'=>'93','pid'=>'92','title'=>'系统信息','link'=>'../blank.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
);
?>
<?php
//$q = "select CONCAT_WS( '_', id , pid) as indexKey , CONCAT_WS('_' , title , link , published , ordering , access , params ) as valueKey from #__collector_menu where access>=0 ";
/**
* 共计查找数据库两次,使用CONCAT_WS函数两次
* 使用正则搜数组一次(N条记录)*N次(N*X条记录)*(N*X)次(Y条记录,依次类推)。
* 对于2级类,则是 2数据库+1*N次正则。
* 如果用数据库,则是1数据库 * N次数据库。
* 如果用数组遍历,则是1数据库(Y条记录)+N*Y+(Y-N)次数组循环。
* 如果用字符串,将所有记录穿成一个字符串,然后用正则递归搜,将面临搜到字串后要分割字串的问题。
* 臆测字串搜起来恐怕比数组搜索要效率高一些。
*
* 不知道哪个效率更高。不过我想,无论如何从内存搜应该比从数据库里搜要快吧
* 而内存中搜,用正则和数组遍历的比较。对于大记录数组遍历的循环次数将会恐怖地增长。
* 然而我不知道preg_grep在数组中搜索是否和普通数组循环一样。如果是,那么。二者恐怕效率差不多。
*
* 比较字符串搜索,数组的效率要高出10倍。27条记录(6x4),。1000次/4秒2005-12-29。
* 其中从数据库取记录+类初始化用了0.004秒时间。而实际执行一次遍历的时间约与此相等0.004秒。
* 经过优化,速度提高了0.001秒 。1000次/3.2秒
*
* 下个版本将对1000条-10000条记录做测试。
*/
//时间开始
$timeStart = getmicrotime();
$tree = new TreeMenu;
$tree->pid=0; //初始化顶级类别父类号
$tree->dim="_"; //需和sql中的CONCAT_WS中使用的一致。
$tree->rowsIndex=&$rowsIndex;
$tree->rows=&$rows;
$tree->start();
echo $tree->menu;
//时间结束
$timeEnd = getmicrotime();
echo "耗费时间:".($timeEnd-$timeStart)." 秒<br/>";
class TreeMenu{
var $rowsIndex;
var $rows;
var $dim="_"; //连接字段的分割符
var $pid;
var $menu='┌◎\n';
/* 开始执行 */
function start(){
/* 搜出所有的$pid下的类别 */
$reg = "#([0-9]+)".$this->dim.$this->pid."#i";
$mainIndex=preg_grep($reg,$this->rowsIndex); //prt($mainIndex);
if (count($mainIndex)==0) {
return;
}
//$n=1;
//for ($i=0;$i<$n ;$i++ ) {
$this->menu="┌◎\n";
$this->getTreeMenuStr(0,0,0,$mainIndex);
//}
}//end func start
/* 生成菜单字符串,存入this->menu */
function getTreeMenuStr($pid=0,$level=0,$blanklevel=0,&$mainIndex){
$pre="";
for ($k=0;$k<$level ;$k++ ) {
$pre .="│";//echot($level);
}
for ($k=0;$k<$blanklevel ;$k++ ) {
$pre .=" ";//echot($level);
}
/* 控制 */
$totmain = count($mainIndex)-1;
$toti=0;
/* 为每一个顶级类别找出其子类别 ├┬*/
foreach ($mainIndex as $mkey=>$vk) {
$nextlevel = $level;
$link = "<a href='".$this->rows[$vk][link]."' >";
$title = $this->rows[$vk]['title'];
/* 在索引数组里找出下级类别 */
$reg = "#([0-9]+)".$this->dim.$mkey."#i";
$subIndex=preg_grep($reg,$this->rowsIndex); //prt($subIndex);
$totsub = count($subIndex);
if ($toti<$totmain) {
$this->menu .=$pre.'├'.$title."\n";
}
else {
$this->menu .=$pre.'└'.$title."\n";
}
if ($totsub>0) {
if ($toti<$totmain) {
$nextlevel=$level+1;
}
else {
$blanklevel++;
}
$this->getTreeMenuStr($mkey,$nextlevel,$blanklevel,$subIndex);
}
$toti++;
}//foreach
//$this->menu .=$pre."└◎\n";
}//end func
}//end class
?>
</body>
</html>
require_once("echo.php");
?>
<html>
<head>
<title>生成多级分类树测试-〉搜array法</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<style>
body {
scrollbar-base-color:#517CEA;scrollbar-arrow-color:#FFFFFF;
font:14px verdana;
}
*{line-height:1;margin:0px;PADDING:0px;}
</style>
<script language="javascript">
function showHide(obj){
var oStyle = obj.parentElement.parentElement.parentElement.rows[1].style;
oStyle.display == "none" ? oStyle.display = "block" : oStyle.display = "none";
}
function showHide2(obj){
var oStyle = obj.parentElement.parentElement.div[1].style;
oStyle.display == "none" ? oStyle.display = "block" : oStyle.display = "none";
}
</script>
<base target="main">
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" target="main">
<pre>
<?php
$rowsIndex=array('66'=>'66_0','67'=>'67_0','68'=>'68_67','69'=>'69_67','70'=>'70_69','71'=>'71_0','72'=>'72_71','73'=>'73_71','74'=>'74_71','75'=>'75_0','76'=>'76_75','77'=>'77_75','78'=>'78_75','79'=>'79_75','80'=>'80_0','81'=>'81_80','82'=>'82_80','83'=>'83_80','84'=>'84_80','85'=>'85_0','86'=>'86_85','87'=>'87_0','88'=>'88_87','89'=>'89_0','90'=>'90_89','91'=>'91_89','92'=>'92_0','93'=>'93_92'
);
$rows=array('66_0'=>
array('indexKey'=>'66_0','id'=>'66','pid'=>'0','title'=>'<a href="#" target="_parent">退出DEDECMS</a>','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'67_0'=>
array('indexKey'=>'67_0','id'=>'67','pid'=>'0','title'=>'频道管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'68_67'=>
array('indexKey'=>'68_67','id'=>'68','pid'=>'67','title'=>'板块模板管理','link'=>'../web_type_web.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'69_67'=>
array('indexKey'=>'69_67','id'=>'69','pid'=>'67','title'=>'通用模板管理','link'=>'../file_view.php?activepath=<?=$mod_dir?>','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'70_69'=>
array('indexKey'=>'70_69','id'=>'70','pid'=>'69','title'=>'主页创建向导','link'=>'../add_home_page.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'71_0'=>
array('indexKey'=>'71_0','id'=>'71','pid'=>'0','title'=>'内容维护','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'72_71'=>
array('indexKey'=>'72_71','id'=>'72','pid'=>'71','title'=>'待审核文章','link'=>'../list_news_member.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'73_71'=>
array('indexKey'=>'73_71','id'=>'73','pid'=>'71','title'=>'专题管理','link'=>'../list_news_spec.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'74_71'=>
array('indexKey'=>'74_71','id'=>'74','pid'=>'71','title'=>'评论管理','link'=>'../list_feedback.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'75_0'=>
array('indexKey'=>'75_0','id'=>'75','pid'=>'0','title'=>'内容发布','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'76_75'=>
array('indexKey'=>'76_75','id'=>'76','pid'=>'75','title'=>'专题创建向导','link'=>'../add_news_spec.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'77_75'=>
array('indexKey'=>'77_75','id'=>'77','pid'=>'75','title'=>'图集发布向导','link'=>'../add_news_pic.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'78_75'=>
array('indexKey'=>'78_75','id'=>'78','pid'=>'75','title'=>'软件发布向导','link'=>'../add_news_soft.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'79_75'=>
array('indexKey'=>'79_75','id'=>'79','pid'=>'75','title'=>'Flash向导','link'=>'../add_news_flash.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'80_0'=>
array('indexKey'=>'80_0','id'=>'80','pid'=>'0','title'=>'附助功能','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'81_80'=>
array('indexKey'=>'81_80','id'=>'81','pid'=>'80','title'=>'友情链接管理','link'=>'../add_friendlink.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'82_80'=>
array('indexKey'=>'82_80','id'=>'82','pid'=>'80','title'=>'投票管理','link'=>'../add_vote.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'83_80'=>
array('indexKey'=>'83_80','id'=>'83','pid'=>'80','title'=>'论坛扩展','link'=>'../bbs_addons.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'84_80'=>
array('indexKey'=>'84_80','id'=>'84','pid'=>'80','title'=>'留言簿管理','link'=>'<?=$art_php_dir?>/guestbook/index.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'85_0'=>
array('indexKey'=>'85_0','id'=>'85','pid'=>'0','title'=>'会员管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'86_85'=>
array('indexKey'=>'86_85','id'=>'86','pid'=>'85','title'=>'网上会员管理','link'=>'../list_user.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'87_0'=>
array('indexKey'=>'87_0','id'=>'87','pid'=>'0','title'=>'文件管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'88_87'=>
array('indexKey'=>'88_87','id'=>'88','pid'=>'87','title'=>'文件浏览器','link'=>'../file_view.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'89_0'=>
array('indexKey'=>'89_0','id'=>'89','pid'=>'0','title'=>'数据库管理','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'90_89'=>
array('indexKey'=>'90_89','id'=>'90','pid'=>'89','title'=>'数据备份/还原','link'=>'../sys_back_data.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'91_89'=>
array('indexKey'=>'91_89','id'=>'91','pid'=>'89','title'=>'MySQL命令','link'=>'../sys_domysql.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'92_0'=>
array('indexKey'=>'92_0','id'=>'92','pid'=>'0','title'=>'系统帮助','link'=>'#','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
,'93_92'=>
array('indexKey'=>'93_92','id'=>'93','pid'=>'92','title'=>'系统信息','link'=>'../blank.php','published'=>'1','ordering'=>'0','access'=>'0','params'=>''
)
);
?>
<?php
//$q = "select CONCAT_WS( '_', id , pid) as indexKey , CONCAT_WS('_' , title , link , published , ordering , access , params ) as valueKey from #__collector_menu where access>=0 ";
/**
* 共计查找数据库两次,使用CONCAT_WS函数两次
* 使用正则搜数组一次(N条记录)*N次(N*X条记录)*(N*X)次(Y条记录,依次类推)。
* 对于2级类,则是 2数据库+1*N次正则。
* 如果用数据库,则是1数据库 * N次数据库。
* 如果用数组遍历,则是1数据库(Y条记录)+N*Y+(Y-N)次数组循环。
* 如果用字符串,将所有记录穿成一个字符串,然后用正则递归搜,将面临搜到字串后要分割字串的问题。
* 臆测字串搜起来恐怕比数组搜索要效率高一些。
*
* 不知道哪个效率更高。不过我想,无论如何从内存搜应该比从数据库里搜要快吧
* 而内存中搜,用正则和数组遍历的比较。对于大记录数组遍历的循环次数将会恐怖地增长。
* 然而我不知道preg_grep在数组中搜索是否和普通数组循环一样。如果是,那么。二者恐怕效率差不多。
*
* 比较字符串搜索,数组的效率要高出10倍。27条记录(6x4),。1000次/4秒2005-12-29。
* 其中从数据库取记录+类初始化用了0.004秒时间。而实际执行一次遍历的时间约与此相等0.004秒。
* 经过优化,速度提高了0.001秒 。1000次/3.2秒
*
* 下个版本将对1000条-10000条记录做测试。
*/
//时间开始
$timeStart = getmicrotime();
$tree = new TreeMenu;
$tree->pid=0; //初始化顶级类别父类号
$tree->dim="_"; //需和sql中的CONCAT_WS中使用的一致。
$tree->rowsIndex=&$rowsIndex;
$tree->rows=&$rows;
$tree->start();
echo $tree->menu;
//时间结束
$timeEnd = getmicrotime();
echo "耗费时间:".($timeEnd-$timeStart)." 秒<br/>";
class TreeMenu{
var $rowsIndex;
var $rows;
var $dim="_"; //连接字段的分割符
var $pid;
var $menu='┌◎\n';
/* 开始执行 */
function start(){
/* 搜出所有的$pid下的类别 */
$reg = "#([0-9]+)".$this->dim.$this->pid."#i";
$mainIndex=preg_grep($reg,$this->rowsIndex); //prt($mainIndex);
if (count($mainIndex)==0) {
return;
}
//$n=1;
//for ($i=0;$i<$n ;$i++ ) {
$this->menu="┌◎\n";
$this->getTreeMenuStr(0,0,0,$mainIndex);
//}
}//end func start
/* 生成菜单字符串,存入this->menu */
function getTreeMenuStr($pid=0,$level=0,$blanklevel=0,&$mainIndex){
$pre="";
for ($k=0;$k<$level ;$k++ ) {
$pre .="│";//echot($level);
}
for ($k=0;$k<$blanklevel ;$k++ ) {
$pre .=" ";//echot($level);
}
/* 控制 */
$totmain = count($mainIndex)-1;
$toti=0;
/* 为每一个顶级类别找出其子类别 ├┬*/
foreach ($mainIndex as $mkey=>$vk) {
$nextlevel = $level;
$link = "<a href='".$this->rows[$vk][link]."' >";
$title = $this->rows[$vk]['title'];
/* 在索引数组里找出下级类别 */
$reg = "#([0-9]+)".$this->dim.$mkey."#i";
$subIndex=preg_grep($reg,$this->rowsIndex); //prt($subIndex);
$totsub = count($subIndex);
if ($toti<$totmain) {
$this->menu .=$pre.'├'.$title."\n";
}
else {
$this->menu .=$pre.'└'.$title."\n";
}
if ($totsub>0) {
if ($toti<$totmain) {
$nextlevel=$level+1;
}
else {
$blanklevel++;
}
$this->getTreeMenuStr($mkey,$nextlevel,$blanklevel,$subIndex);
}
$toti++;
}//foreach
//$this->menu .=$pre."└◎\n";
}//end func
}//end class
?>
</body>
</html>