php生成二叉树
数据结构和简单效果。
class datatree{
private $gui=2; //规
private $ceng=2; //层
private $maxcle; //列
private $default_data=array();//默认数据
private $mark=array();// 关联标记
public $error=null;
private $table_w=800; //表格宽度
protected $data_arr=array();
function __construct($gui,$ceng,$mark){
if(intval($gui)>0){
$this->gui=intval($gui);
}
if(intval($ceng)>0){
$this->ceng=intval($ceng);
}
if(is_string($mark)){
$mark=explode(',', $mark);
}
if($this->gui!=count($mark)){
$this->error="标记数量不正确,无法获取数据";
}else{
$this->mark=$mark;
}
$this->maxcle=pow($this->gui,$this->ceng-1);
//初始默认值
$this->default_data['username']='未注册';
$this->default_data['status']='---';
$this->default_data['_leftmoney']=0;
$this->default_data['_rightmoney']=0;
$this->default_data['leftmoney']=0;
$this->default_data['rightmoney']=0;
//初始默认值
}
function setDefault($data=array()){
if($this->error) return $this->error;
$this->default_data=$data;
}
function getTreeData($topuser){ //传入顶层人员数据
if($this->error) return $this->error;
for($ci=1;$ci<=$this->ceng;$ci++){ //层数循环
for($li=1;$li<= pow($this->gui,$ci-1);$li++){//元素循环
if($ci==1){
$this->data_arr[$ci][$li]=$topuser;
}else{
$last_c=$ci-1; //上一层
$last_li= ceil($li/$this->gui); //上一层 第几个人
$now_li= $li>3 ? fmod($li,$this->gui) : $li ; //上一层 第几个人 第几个区
$now_li=$now_li-1;
if( is_array($this->data_arr[$last_c][$last_li]) && $this->data_arr[$last_c][$last_li][$this->mark[$now_li]]){
$where['username']=$this->data_arr[$last_c][$last_li][$this->mark[$now_li]];
$member_data=M("member")->where($where)->find();
}else{
$member_data=false;
}
$this->data_arr[$ci][$li]=$member_data;
}
}
}
}
function buildhtml($topuser=''){
if($this->error) return $this->error;
if(!$this->data_arr){
$this->getTreeData($topuser);
}
$html ='';
$html .=''">';
for($ci=1;$ci<=$this->ceng;$ci++){ //层数循环
$html .='';
for($li=1;$li<= pow($this->gui,$ci-1);$li++){//元素循环
$html .='';
}
$html .='';
}
$html .=''" align="center">';
$html .= $this->format($ci,$li);
$html .= $this->line($ci);
$html .='
';
return $html;
}
protected function line($ci){
$temp_htlm='';
if($ci!=$this->ceng){
$temp_htlm ='';
for($line_i=1;$line_i<=$this->gui;$line_i++){
if($line_i==1){
$temp_htlm .='';
}elseif($line_i==$this->gui){
$temp_htlm .='';
}else{
$temp_htlm .='';
}
}
$temp_htlm .='';
}
return $temp_htlm;
}
protected function format($ci,$li){
$data = $this->data_arr[$ci][$li];
if($data===false){
$data=$this->default_data;
$title_link='未注册';
}else{
$title_link=''.$data['username'].'';
}
$temp_htlm = '';
$temp_htlm .= '';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= '';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= '';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= '';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= ' ';
$temp_htlm .= ''.$title_link.' '.$data['status'].' '.$data['_leftmoney'].' '.$data['_rightmoney'].' '.$data['_othermoney'].' '.$data['leftmoney'].' '.$data['rightmoney'].' '.$data['othermoney'].'
';
return $temp_htlm;
}
}
调用方法:
$topuser = M("member")->where("uid=6")->find();
include APP_ROOT_PATH.'Plugin/libs/datatree.php'; //引用类
$datatree = new \datatree(2,3,'_left,_right'); //实例化
echo $datatree->buildhtml($topuser); //传入顶层 生成html