首页 > 编程技术 > php

php 无限级分类函数

发布时间:2016-11-25 16:29

 

ar=array(0=>array('name'=>'食物','id'=>1,'pid'=>0), 

02               1=>array('name'=>'植物','id'=>2,'pid'=>0), 

03               3=>array('name'=>'白菜','id'=>3,'pid'=>1), 

04               4=>array('name'=>'树','id'=>4,'pid'=>2), 

05               5=>array('name'=>'苹果','id'=>5,'pid'=>3), 

06               6=>array('name'=>'松树','id'=>6,'pid'=>4), 

07               7=>array('name'=>'饮料','id'=>7,'pid'=>1), 

08               8=>array('name'=>'测试1','id'=>8,'pid'=>7), 

09               9=>array('name'=>'测试2','id'=>9,'pid'=>8), 

10               10=>array('name'=>'人民','id'=>10,'pid'=>9), 

11               11=>array('name'=>'小米','id'=>11,'pid'=>0), 

12               ); 

13   

14         echo '<table border="1" width="100%">'; 

15         tre($ar,0); 

16         echo '</table>'; 

17   

18         function tre($tree,$id) 

19         { 

20                 $str = ''; 

21                 $strid = ''; 

22                 foreach ($tree as $key => $item) 

23                 { 

24                         if ($item['pid'] == $id) { 

25                                 $i = 0; 

26                                 $i = sonNum($tree,$item['id']); 

27                                 if ($i == 1 || $i == 0) { 

28                                         echo '<tr><td>'; 

29                                 } else { 

30                                         echo '<tr><td rowspan="'.$i.'">';         

31                                 } 

32                                 //echo '<tr><td>'; 

33                                 echo $item['name']; 

34                                 echo '</td></tr>'; 

35                                 tre($tree,$item['id']); 

36                         } 

37                 } 

38                 //tre($tree,$strid); 

39         } 

40           

41         function sonNum($tre,$sum,$totale=1) 

42         { 

43                 foreach ($tre as $key => $value) 

44                 { 

45                         if ($value['pid'] == $sum){ 

46                                 $totale += sonNum($tre,$value['id'],1); 

47                         } 

48                 } 

49                 return $totale; 

50         }

//$pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 这个正则表达式,对文件夹的月日进行捕获,再进一步判断处理 

02 for($i =2005;$i<=2009;$i++) 

03 { 

04 for($j = 1;$j<30;$j++) 

05 { 

06 $dest_dir = "text/file-".$i."-$j"; 

07 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

08 } 

09 } 

10 //创建一些不满足条件的文件夹 

11   

12 for($t = 0;$t<10;$t++) 

13 { 

14 $dest_dir = "text/file-".$t; 

15 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

16 } 

17   

18 //遍历文件夹处理 . 

19 function listFile($dir) 

20 {  

21 $a = array(); 

22 $handle = opendir($dir); 

23 while($file = readdir($handle)) 

24 { 

25 handle_dir($dir,$file,&$a); 

26 } 

27   

28 echo "the rege_array is "; 

29 print("<pre>"); 

30 print_r($a); 

31 print("</pre>"); 

32 } 

33   

34 function handle_dir($dir,$file,$a) 

35 { 

36   

37 if($file == "." || $file == "..") return ; 

38 $minYear = 2008; 

39 $maxDay = 13; 

40   

41 $pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 

42 $destPath = $dir."/".$file; 

43   

44 if(!is_dir($destPath)) 

45 { 

46 //echo "$destPath is not a dir ;"; 

47 return; 

48 } 

49   

50 //删除不满足格式的文件 

51 if(!preg_match($pattern,$file)) 

52 { 

53 //echo "<font color=blue>$file</font><font color=red>is not the right rege. the program will unlink -- $destPath --</font><br/>"; 

54 // unlink($destPath); 

55 return; 

56 } 

57   

58 preg_match_all($pattern,$file,$matchs); 

59   

60 //echo "<font color=blue>$file</font> is the right rege"; 

61 //print_r($matchs); 

62 //echo "the year is ".$matchs[1][0]." and the day is ".$matchs[2][0]."</br>"; 

63 if(intval($matchs[1][0]) >$minYear && intval($matchs[2][0]) < $maxDay) 

64 { 

65 $a[]= $destPath; 

66 }else { //不满足条件的 

67 //unlink($destPath); 

68   

69 } 

70 } 

71   

72 listFile("text");

对于php文件操作那么关于在指定的位置插入数据就比较复杂了,下面我们就来看看关系在文件指定行插入数据实例吧。

$arrInsert = insertContent("array.php", "abcdef", 3, 10);
unlink("array.php");
foreach($arrInsert as $value)
{
    file_put_contents("array.php", $value, FILE_APPEND);
}
 
 
function insertContent($source, $s, $iLine, $index) {
    $file_handle = fopen($source, "r");
    $i = 0;
    $arr = array();
    while (!feof($file_handle)) {
       
       $line = fgets($file_handle);
       ++$i;
       if ($i == $iLine) {
            if($index == strlen($line)-1)
                $arr[] = substr($line, 0, strlen($line)-1) . $s . " ";
            else
                $arr[] = substr($line, 0, $index) . $s . substr($line, $index);
       }else {
       
               $arr[] = $line;
       }
    }
    fclose($file_handle);
    return $arr;
}
//在多数据我们存储数据都是用数据库教程来操作,上面我们就是把数据以X格式存在文本中了,现在我要像操作数据库一样的,想删除那行就那行,保存数据也一样,怎么读取第几行就第几行了,所以我就写出来了php 在文件指定行插入数据实例哦。
?>
$iLine:为第几行,$index为第几个字符之前

<?
//声明一个final类Math
class Math{
const PI = 3.14;
public function __toString(){
return "这是Math类。";
}
//这里写了一个算圆面积的方法.使用了Const常量,
//注意使用的方法,类似与静态变量.
public final function areaOfCircular($r){
return $r * $r * self::PI ;
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
public function setPI($a){
self::PI = 3.1415;
}
}
echo Math::PI ;
?>

Parse error: parse error in E:PHPProjects est.php教程 on line 17


<?
//声明一个final类Math
class Math{
const PI = 3.14;
public function __toString(){
return "这是Math类。";
}
//这里写了一个算圆面积的方法.使用了Const常量,
//注意使用的方法,类似与静态变量.
public final function areaOfCircular($r){
return $r * $r * self::PI ;
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
echo Math::PI ;
?>

在PHP5中 const定义的常量与定义变量的方法不同,不需要加 $ 修饰符。const PI = 3.14; 这样就可以。
而使用const 定义的常量名称一般都大写,这是一个约定,在任何语言中都是这样。
如果定义的常量由多个单词组成,使用 _ 连接,这也是约定。
比如, MAX_MUMBER 这样的命名方式。一个良好的命名方式,是程序员必须注意的。
类中的常量使用起来类似静态变量,不同点只是它的值不能被改变。我们使用 类名::常量名 来调用这个常量。

 

Fatal error: Class SuperMath may not inherit from final class (Math) in E:PHPProjects est.php教程 on line 14


<?
//声明一个final类Math
class Math{
public static $pi = 3.14;
public function __toString(){
return "这是Math类。";
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
//声明类SuperMath 继承自 Math类
class SuperMath extends Math {
public final function max($a,$b){}
}
//执行会出错,final方法不能被重写。

?>
<?
//声明一个final类Math
final class Math{
public static $pi = 3.14;

public function __toString(){
return "这是Math类。";
}
}
$math = new Math();
echo $math;

//声明类SuperMath 继承自 Math类
class SuperMath extends Math {
}
//执行会出错,final类不能被继承。

?>

Fatal error: Class SuperMath may not inherit from final class (Math) in E:PHPProjects est.php on line 16

标签:[!--infotagslink--]

您可能感兴趣的文章: