当前位置: 首页 > 图文教程 > 网络编程 > PHP > php:树形结构的算法 4

PHP
一贴学会PHP 新手入门教程
用PHP的ob_start() 控制您的浏览器cache
谈谈新手如何学习PHP 默默经典版本
黑夜路人出的几道php笔试题
一些 PHP 管理系统程序中的后门
用php获取本周,上周,本月,上月,本季度日期的代码
PHP 简单数组排序实现代码
PHP 多维数组排序实现代码
php 全局变量范围分析
php_xmlhttp 乱码问题解决方法
PHP 数组学习排序全接触
php Sql Server连接失败问题及解决办法
PHP 翻页 实例代码
php 随机数的产生、页面跳转、件读写、文件重命名、switch语句
PHP 5.3.0 安装分析心得
php 生成WML页面方法详解
php 取得瑞年与平年的天数的代码
php empty函数 使用说明
php natsort内核函数浅析
PHP 源代码分析 Zend HashTable详解

PHP 中的 php:树形结构的算法 4


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 193 ::
收藏到网摘: n/a

  <?php
  /**
  * 显示列表
  * @access public
  */
  function DispList()
  {
  //不缩进的显示方式
  // $this->mIsDispListIndex = true;
  // echo('<p align="right"><a href="?action=new&part=role">增加新角色</a>  </p>'); _fcksavedurl=""?action=new&part=role">增加新角色</a>  </p>');"
  //
  // $this->mListTitle = '用户角色列表';
  // $this->SetDataOption('list');
  //
  // $this->SetQueryTable( array($this->mTableUserRole) );
  //
  // //查询顺序
  // $this->SetQueryOrder( 'asc', $this->mTableUserRole, 'sequence' );
  //
  // $this->Query('list');
  // parent::DispList();
  
  // //另外一种显示方式,用数组作为堆栈,A: 压栈时存role,压完就删除source
  // $this->CheckProperty('mrDb');
  // $this->CheckProperty('mrSql');
  // $this->mrSql->Select('role, title, parent');
  // $this->mrSql->From($this->mTableUserRole);
  // $this->mrSql->Orderby('parent, sequence');
  // $this->mRs = $this->mrDb->Execute($this->mrSql->Sql());
  // if (0 < count($this->mRs))
  // {
  // $source = & $this->mRs->GetArray(); //数字索引
  // $stack = array(''); //堆栈
  // $stacki = array(-1); //和堆栈对应,记录堆栈中数据在树中的层次
  // $target = array();
  // while (0 < count($stack))
  // {
  // $item = array_shift($stack);
  // $lev = array_shift($stacki);
  // if (!empty($item))
  // {
  // //在这里把加工过的数据放到target数组
  // array_push($target, str_repeat(' ', $lev) . $item);
  // //$s1 = str_repeat(' ', $lev) . $item;
  // }
  // $del = array(); //要从$source中删除的节点
  // $ar = array(); //需要添加到堆栈中的节点
  // foreach ($source as $key=>$val)
  // {
  // //寻找匹配的子节点
  // if (empty($item))
  // {
  // $find = empty($source[$key]['parent']);
  // }
  // else
  // {
  // $find = ($item == $source[$key]['parent']);
  // }
  // if ($find)
  // {
  // array_unshift($ar, $source[$key]['role']);
  // $del[] = $key;
  // }
  // }
  // foreach ($ar as $val)
  // {
  // array_unshift($stack, $val);
  // array_unshift($stacki, $lev + 1);
  // }
  // foreach ($del as $val)
  // {
  // unset($source[$val]);
  // }
  // echo(implode(', ', $stack) . '<br />' . implode(', ', $stacki) . '<br />' . implode(', ', $target) . '<br /><br />');
  // }
  // debug_array();
  // }
  // else
  // {
  // echo('<center>没有检索到数据</center>');
  // }
  
  //另外一种显示方式,用数组作为堆栈,B: 压栈时存数组索引,出栈并使用完后再删除source
  $this->CheckProperty('mrDb');
  $this->CheckProperty('mrSql');
  $this->mrSql->Select('role, title, parent');
  $this->mrSql->From($this->mTableUserRole);
  $this->mrSql->Orderby('parent, sequence');
  $this->mRs = $this->mrDb->Execute($this->mrSql->Sql());
  if (!empty($this->mRs) && !$this->mRs->EOF)
  {
  $source = & $this->mRs->GetArray(); //数字索引
  $stack = array(-1); //堆栈
  $stacki = array(-1); //和堆栈对应,记录堆栈中数据在树中的层次
  $target = array();
  while (0 < count($stack))
  {
  $item = array_shift($stack);
  $lev = array_shift($stacki);
  if (-1 != $item)
  {
  //在这里把加工过的数据放到target数组
  $s1 = str_repeat(' ', $lev) . '<a href="?action=disp&part=role&role=' . $source[$item]['role'] . '">' . $source[$item]['title'] . '</a>';
  $s2 = '<a href="?action=edit&part=role&role=' . $source[$item]['role'] . '">编辑</a> <a href="?action=delete&part=role&role=' . $source[$item]['role'] . '">删除</a>';
  array_push($target, array($s1, $s2));
  }
  $del = array(); //要从$source中删除的节点
  $ar = array(); //需要添加到堆栈中的节点
  foreach ($source as $key=>$val)
  {
  //寻找匹配的子节点
  if (-1 == $item)
  {
  $find = empty($source[$key]['parent']);
  }
  else
  {
  $find = ($source[$item]['role'] == $source[$key]['parent']);
  }
  if ($find)
  {
  array_unshift($ar, $key);
  }
  }
  foreach ($ar as $val)
  {
  array_unshift($stack, $val);
  array_unshift($stacki, $lev + 1);
  }
  //从source中删除
  unset($source[$item]);
  //echo(implode(', ', $stack) . '<br />' . implode(', ', $stacki) . '<br />' . implode(', ', $target) . '<br /><br />');
  }
  //输出
  echo('<p align="right"><a href="?action=new&part=role">增加新角色</a>  </p>');
  array_unshift($target, array('角色', '操作'));
  $this->CheckProperty('mrLt');
  $this->mrLt->SetData($target);
  $this->mrLt->mListTitle = '用户角色列表';
  $this->mrLt->mIsDispIndex = false;
  $this->mrLt->Disp();
  }
  else
  {
  echo('<center>没有检索到数据</center>');
  }
  } // end of function DispList
  ?>