教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP无限分级数据实现程序代码

PHP无限分级数据实现程序代码

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供PHP无限分级数据实现程序代码等资源,欢迎您收藏本站,我们将为您提供最新的PHP无限分级数据实现程序代码资源
我们来给大家介绍一下关于在php中无发限分类的一些用法,包括有直接无限分类数据库与数组的操作,有需要的朋友可参考参考。

先看一个php与mysql数据库的无限分类


建立数据库 :
id,fid,fname(id和fid都必需是数字类型而且fid的默认值一定要设成0;

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1492')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1492>

<?php
$stime=microtime();

$db=@mysql_connect("localhost","root","micronsky.net")   or die("数据库连接失入");
mysql_select_db("temp",$db);
//定义第一级分类
function mainfl()
{
     global $db;
$result=mysql_query("select id,fid,fname from wxjfl where fid=0   order by id desc",$db);
if ($myrow=mysql_fetch_array($result)) {
   do {
   echo $myrow["fname"];
   echo "<br>";
   echo subfl($myrow["id"],"  ");   //此处调用子级分类函数
   }
   while ($myrow=mysql_fetch_array($result));
}

}
//定义子级分类
function subfl($fid,$strdis)
{
global $db;
$result1=mysql_query("select id,fid,fname from wxjfl where fid=$fid   order by id desc",$db);
if ($myrow1=mysql_fetch_array($result1)) {
do {
   //echo $strdis;
   echo $strdis.$myrow1["fname"];
   echo "<br>";
   subfl($myrow1["id"],"  ".$strdis); //这里一定要注意并不需要像上面那个函数那样echo ...只要直接调用子级分类函数并付值就行了!同时这里也是递归部分
}
while ($myrow1=mysql_fetch_array($result1));
}
}


echo mainfl();

$ltime=microtime();
echo "<br>";
echo number_format($ltime-$stime,4);   //统计执行时间,这里就比ASP快多了,但这跟写法没多大关系,主要是PHP本身加入了加速器的原因!

?>


现在我们直接利用数组进行分页

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6501')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6501>

/**
 * 创建父节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 **/
function find_parent($ar, $id='id', $pid='pid') {
  foreach($ar as $v) $t[$v[$id]] = $v;
  foreach ($t as $k => $item){
    if( $item[$pid] ){
      if( ! isset($t[$item[$pid]]['parent'][$item[$pid]]) )
         $t[$item[$id]]['parent'][$item[$pid]] =& $t[$item[$pid]];

          $t[$k]['reference'] = true;
    }
  }
  return $t;
}


/**
 * 创建子节点树形数组
 * 参数
 * $ar 数组,邻接列表方式组织的数据
 * $id 数组中作为主键的下标或关联键名
 * $pid 数组中作为父键的下标或关联键名
 * 返回 多维数组
 **/
function find_child($ar, $id='id', $pid='pid') {
  foreach($ar as $v) $t[$v[$id]] = $v;
  foreach ($t as $k => $item){
    if( $item[$pid] ) {
      $t[$item[$pid]]['child'][$item[$id]] =& $t[$k];

      $t[$k]['reference'] = true;
    }
  }
  return $t;
}

 

示例:

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1685')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1685>$data = array(
    array('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'),
    array('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'),
    array('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'),
    array('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'),
    array('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子')
);
$p = find_parent($data, 'ID', 'PARENT');
$c = find_child($data, 'ID', 'PARENT');

上面两种方法是将所有节点按id平摊到一个数组中,然后找到他们的 parent 或 children ,通过引用将 平摊的元素挂接到 parent 、children 下,

但被引用的元素依然存在于平摊的数组中,因此,在实际应用时,最好标记那些被引用的元素,以避免以他们为根开始遍历,导致重复。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4260')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4260>


        foreach ($p as $key => $item) {
            if($item['reference']) continue;
            print_r($item);
        }

        foreach ($c as $key => $item) {
            if($item['reference']) continue;
            print_r($item);
        }

 

递归法,PHP 数组元素被删除后,数组游标会归零,因此在遍历过程中一些已经找到 “归宿” 的元素也不得不留在数组中,无法缩减后继元素的搜索范围:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9164')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9164>


$mylist = array(array( 'parent_id'=>0,'id'=>1),
                    array( 'parent_id'=>0,'id'=>2),
                    array( 'parent_id'=>0,'id'=>3),   
                    array( 'parent_id'=>2,'id'=>4),
                    array( 'parent_id'=>2,'id'=>5),
                    array( 'parent_id'=>3,'id'=>6),
                    array( 'parent_id'=>3,'id'=>7),   
                    array( 'parent_id'=>4,'id'=>8),
                    array( 'parent_id'=>5,'id'=>9),
                    array( 'parent_id'=>5,'id'=>10)
                );

 

    function _findChildren($list, $p_id){    //数据层级化,
          $r = array();
          foreach($list as $id=>$item){
            if($item['parent_id'] == $p_id) {
                   $length = count($r);
                  $r[$length] = $item;
                  if($t = $this->_findChildren($list, $item['id']) ){
                      $r[$length]['children'] = $t;
                  }               
            }
          }
          return $r;
    } 
 

print_r(_findChildren($mylist, 0));

您可能感兴趣的文章:
php用递归方法实现无限级分类的代码
php无限级分类的递归函数
PHP用递归返回无限级分类回字符串或数组实例
php递归实现无限分类生成下拉列表函数代码
PHP无限级分类菜单实例程序
php与mysql实现的无限级分类
php 无限极分类(递归函数)代码
php递归方法实现无限分类示例
php写的一个递归实现无限分类生成下拉列表的函数
PHP 无限分类程序代码

[关闭]
~ ~