Home > php教程 > php手册 > body text

php:树形结构的算法 4

WBOY
Release: 2016-06-21 08:57:55
Original
4145 people have browsed it

    /**
  * 显示列表
  * @access public
  */
  function DispList()
  {
  //不缩进的显示方式
  // $this->mIsDispListIndex = true;
  // echo('

增加新角色  

'); _fcksavedurl=""?action=new&part=role">增加新角色  ');"
  //
  // $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 mRs))
  // {
  // $source = & $this->mRs->GetArray(); //数字索引
  // $stack = array(''); //堆栈
  // $stacki = array(-1); //和堆栈对应,记录堆栈中数据在树中的层次
  // $target = array();
  // while (0   // {
  // $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) . '
' . implode(', ', $stacki) . '
' . implode(', ', $target) . '

');
  // }
  // debug_array();
  // }
  // else
  // {
  // echo('
没有检索到数据
');
  // }
  
  //另外一种显示方式,用数组作为堆栈,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   {
  $item = array_shift($stack);
  $lev = array_shift($stacki);
  if (-1 != $item)
  {
  //在这里把加工过的数据放到target数组
  $s1 = str_repeat(' ', $lev) . '' . $source[$item]['title'] . '';
  $s2 = '编辑 删除';
  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) . '
' . implode(', ', $stacki) . '
' . implode(', ', $target) . '

');
  }
  //输出
  echo('

增加新角色  

');
  array_unshift($target, array('角色', '操作'));
  $this->CheckProperty('mrLt');
  $this->mrLt->SetData($target);
  $this->mrLt->mListTitle = '用户角色列表';
  $this->mrLt->mIsDispIndex = false;
  $this->mrLt->Disp();
  }
  else
  {
  echo('
没有检索到数据
');
  }
  } // end of function DispList
  ?>



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template