首页 > 学院 > 逻辑算法 > 正文

php:树形结构的算法 4

2020-02-06 17:06:30
字体:
来源:转载
供稿:网友
  <?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
  ?>



上一篇:返回列表

下一篇:php:树形结构的算法 3

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选