php 无限级分类函数
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
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23
- PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
- 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性。那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类。 递归,简单的说就是一段程序代码的重复调用,当把...2015-10-23
SQL Server中row_number函数的常见用法示例详解
这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
- ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21