Sunday,September-25th
 

php对目录控制

php遍历目录

php+mysql无限级分类(非递归) 要实现无限级分类,递归一般是第一个也是最容易想到的,但是递归一般被认为占用资源的方法,所以很多系统是不考虑使用递归的 本文还是通过数据库的设计,用一句sql语句实现 数据库字段大概如下: ----------------------------------------------------------------------------------- id 编号 fid 父分类编号 class_name 分类名 path 分类路径,以 id 为节点,组成类似 ,1,2,3,4, 这样的字符串 ----------------------------------------------------------------------------------

可以假设有如下的数据

id fid class_name path ---------------------------------------------------- 1  0       分类1 ,       1, 2  0       分类2 ,       2, 3  1       分类1-1 ,    1,3, 4  1       分类1-2 ,    1,4, 5  2       分类2-1 ,    2,5, 6  4       分类1-2-1 , 1,4,6, ----------------------------------------------------

<?php      $sql="SELECT * FROM tree order by path";    $result=$nbs->Query($sql);    while($rows=$nbs->fetch_array($result)){        if(substr_count($rows['path'],',')>2){            for($i=0;$i<(substr_count($rows['path'],',')-2);$i++)                echo ' ';        }        echo $rows['class_name'].'<br>';    }    ?>   

其中$nbs是数据库操作类,此方法简单明了!

 

 遍历文件夹下的所有文件和子文件夹

 

function my_scandir($dir)   {        $files = array();        if ( $handle = opendir($dir) ) {            while ( ($file = readdir($handle)) !== false ) {                if ( $file != ".." && $file != "." ) {                    if ( is_dir($dir . "/" . $file) ) {                        $files[$file] = scandir($dir . "/" . $file);                    }else {                        $files[] = $file;                    }                }            }            closedir($handle);            return $files;        }   }  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script> function $mgo(id){return document.getElementById(id);} function $tag(ta){return document.getElementsByTagName(ta);} function unshow(id){$mgo(id).style.display="none";} function show(id){$mgo(id).style.display="block";}

function chshow(id){         if(  $mgo(id).style.display=="none" || $mgo(id).style.display =="" ){                 $mgo(id).style.display="block";         }         else         {         $mgo(id).style.display="none";         } }

</script>

<script> function tagunshow(t){ var obj=$tag(t)         for(i=0;i<obj.length;i++){         obj[i].style.display="none";         } } function tagshow(t){ var obj=$tag(t)         for(i=0;i<obj.length;i++){         obj[i].style.display="block";         } } window.onload=function(){ //tagunshow("ul"); tagunshow("li"); } </script> <style> span {cursor: /* pointer*/ } </style> </head>

<body> <input name="" type="button"  onclick="tagshow('ul'); tagshow('li');;" value="show all"/><br /><input name="" type="button"  onclick=" tagunshow('li');;" value="unshow all"/><br /> <?php require_once('db_mysql_classB.php'); ?> 作者:heshengyu<br> Email:zfxwhc@163.com<br> data:2009-08-26 18:30<br> <?php

$dbobj=new Mydb();

function myreadfile($filename){         $file = fopen($filename,"r");         $ret;         while(! feof($file)){         $ret=$ret.fgets($file)."  ";         } fclose($file); return $ret; } //$co=myreadfile("sql2.txt"); $co=" create database diana; use diana; DROP TABLE IF EXISTS `cdb_forums`; CREATE TABLE `cdb_forums` (   `fid` smallint(6) unsigned NOT NULL auto_increment,   `fup` smallint(6) unsigned NOT NULL default '0',   `type` enum('group','forum','sub') NOT NULL default 'forum',   `name` char(50) NOT NULL default '',   `status` tinyint(1) NOT NULL default '0',   `displayorder` smallint(6) NOT NULL default '0',   `styleid` smallint(6) unsigned NOT NULL default '0',   `threads` mediumint(8) unsigned NOT NULL default '0',   `posts` mediumint(8) unsigned NOT NULL default '0',   `todayposts` mediumint(8) unsigned NOT NULL default '0',   `lastpost` char(110) NOT NULL default '',   `allowsmilies` tinyint(1) NOT NULL default '0',   `allowhtml` tinyint(1) NOT NULL default '0',   `allowbbcode` tinyint(1) NOT NULL default '0',   `allowimgcode` tinyint(1) NOT NULL default '0',   `allowmediacode` tinyint(1) NOT NULL default '0',   `allowanonymous` tinyint(1) NOT NULL default '0',   `allowshare` tinyint(1) NOT NULL default '0',   `allowpostspecial` smallint(6) unsigned NOT NULL default '0',   `allowspecialonly` tinyint(1) unsigned NOT NULL default '0',   `alloweditrules` tinyint(1) NOT NULL default '0',   `allowfeed` tinyint(1) NOT NULL default '1',   `recyclebin` tinyint(1) NOT NULL default '0',   `modnewposts` tinyint(1) NOT NULL default '0',   `jammer` tinyint(1) NOT NULL default '0',   `disablewatermark` tinyint(1) NOT NULL default '0',   `inheritedmod` tinyint(1) NOT NULL default '0',   `autoclose` smallint(6) NOT NULL default '0',   `forumcolumns` tinyint(3) unsigned NOT NULL default '0',   `threadcaches` tinyint(1) NOT NULL default '0',   `alloweditpost` tinyint(1) unsigned NOT NULL default '1',   `simple` tinyint(1) unsigned NOT NULL,   `modworks` tinyint(1) unsigned NOT NULL,   `allowtag` tinyint(1) NOT NULL default '1',   `allowglobalstick` tinyint(1) NOT NULL default '1',   PRIMARY KEY  (`fid`),   KEY `forum` (`status`,`type`,`displayorder`),   KEY `fup` (`fup`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=gbk;

INSERT INTO `cdb_forums` (fup,name) VALUES ('0','j1_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('0','j1_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('0','j1_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('2','j1_2-j2_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('2','j1_2-j2_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('2','j1_2-j2_3');

 

INSERT INTO `cdb_forums` (fup,name) VALUES ('5','j3_1'); INSERT INTO `cdb_forums` (fup,name) VALUES ('5','j3_2'); INSERT INTO `cdb_forums` (fup,name) VALUES ('5','j3_3'); INSERT INTO `cdb_forums` (fup,name) VALUES ('5','j3_4'); INSERT INTO `cdb_forums` (fup,name) VALUES ('5','j3_5');

INSERT INTO `cdb_forums` (fup,name) VALUES ('6','j3_1'); INSERT INTO `cdb_forums` (fup,name) VALUES ('6','j3_2'); INSERT INTO `cdb_forums` (fup,name) VALUES ('6','j3_3'); INSERT INTO `cdb_forums` (fup,name) VALUES ('6','j3_4'); INSERT INTO `cdb_forums` (fup,name) VALUES ('6','j3_5');

INSERT INTO `cdb_forums` (fup,name) VALUES ('10','j4_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('10','j4_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('10','j4_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('10','j4_4');

INSERT INTO `cdb_forums` (fup,name) VALUES ('19','j4_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('19','j4_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('19','j4_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('19','j4_4');

INSERT INTO `cdb_forums` (fup,name) VALUES ('22','j4_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('22','j4_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('22','j4_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('22','j4_4');

INSERT INTO `cdb_forums` (fup,name) VALUES ('27','j4_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('27','j4_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('27','j4_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('27','j4_4');

 

INSERT INTO `cdb_forums` (fup,name) VALUES ('30','j4_1');

INSERT INTO `cdb_forums` (fup,name) VALUES ('30','j4_2');

INSERT INTO `cdb_forums` (fup,name) VALUES ('30','j4_3');

INSERT INTO `cdb_forums` (fup,name) VALUES ('30','j4_4');

 

";

 

$dbobj->batquery($co);

$co2=" use diana; "; $dbobj->batquery($co2);

$fup=0;

function eacharr($arr){ if(!is_array($arr)){return $arr;} $ret="";     foreach($arr as $key=>$p){         $ret.=$key."==>".$p.", ";         }                  return $ret."<br>".dwonclick($arr);        }

function getChildFid($fup){//传入版块的id 得到 子版块id组成的数组; //echo "<font color=red>【[「</font><br>getChildFid:\$fup的值为:".$fup."<br>"; //echo eacharr($fup)."<br><font color=red>」</font><br>";         $retarr=array();         $db=new Mydb();         $result=$db->batquery("use diana;   select fid from cdb_forums where fup=$fup ");                 if( !empty($result) ){                         while($row=mysql_fetch_array($result)){                                $retarr[]=$row[0];                                }                 }else{echo "没有子版块!<br>";}         $db->close();         //if(count($retarr)==0){return false;}         return $retarr; }

function dwonclick($arrid){ if(!is_array($arrid)){return "";} $ret="";     foreach($arrid as $key=>$p){         $ret.= ('chshow(\''.$p.'\')'.';');         }                $retresult= '<span onclick="'.$ret.'"'.'  onmouseover="this.style.cursor=\'help\'">[+]</span>'; return $retresult; }

$mysqlarr= getChildFid(0);

 

global $loop;

function myview2($arr){//参数为id 数成的数组,比如说数组里全是一级版块的id;                 if($loop>25){exit();echo "die";}         echo "<font color=green >{".$loop."}</font>";         if(/*is_array($arr)&& isset($arr) && */ !empty($arr)  )         {   echo "<ul>";         echo eacharr($arr);                 foreach($arr as $p ){                                 echo "<li id=$p >";                 eval('echo "\$p:{".$p."}<br>";');                         if(isset($p) && !empty($p))                         {                                 echo "\$p有值!\$p:[";                                 echo eacharr($p)."]<br>";                                 $getsqla=getChildFid($p);                                 if($getsqla){                                         $loop++;                                         echo "{<font color=blue>loop:".$loop."</font>}";//不知道为什么这里的$loop总是不加  上面用了$loop++; 可是这值总是不加的;不知道原因;                                         //////echo "\$getsqla的值为:".eacharr($getsqla )."<br>";                                         myview2($getsqla );                                 }                         }                                echo "</li><br>";                 }                 echo "</ul><br>";         }else{echo "没有子版块<br>";}

} myview2($mysqlarr);

 

echo "不知道为什么\$loop的值总是为1明明用了$loop++";

echo "不知道为什么我用递归老是死循环,这个类似print_r  ,<br>但又不像,因为这不是从数组里面取值的 ,<br>我想用js写出一个树目录,来循环历遍 window  对象 window对象是数组里面套数组,<br> 以前写用递归总是死循环,也不知道哪里错了.估计用php写一个树目录估计比这个版块目录更难写,<br>估计又要死循环了.

js的变量特性好像与php不太一样.在php里行得通的在js里不一定行得通.<br> 因为很费精力所以不想去试了!  ";

 

 

?> </body> </html>

附件:Tree AJAX 目录树    Cross Browser Tree Widget 1.17                       

 
附件下载:
1197075581_9946ba12.zip 6.14KB
js-tree.txt 23.61KB
标签: 目录 tree

引用地址:

 

发布留言

 

Links: BaiDu | GooGle | BING | webQQ | iFeng | SkyCN | 360 | TuDou | PcOnLine | Wikipedia | GuoXue | PKU | eshukan | ZGwww | ...