这个类其实主要是为了后台管理写的,也捎带考虑了前台显示问题,粗略写的,以后有时间还要优化
用了PDO请大家自行开启,生成过程只查询一次数据库,比以往递归效率和灵活性应该高点,因为用了继承
也连Config类也一并发出来吧,代码精简自行修改。
PS:现在已经跟新了2个功能,生成Select下拉菜单,生成js数组,我做成了仿win开始程序的功能,有需要的Q我好了,
QQ:252319874
2008-03-29更新:
1.生成下拉树状菜单。
2.生成js无限分类数组。
3.若干public方法,取得类别名,当前类别父路径等,请大家自行根据方法名称试验
PS:去年写的今年发出来,很久没用了,好多方法没写注释,我都忘了是做什么的了,呵呵,
PHP代码:
<?php
define("SITE_PATH",$_SERVER["DOCUMENT_ROOT"]);
class Config
{
const dbtype="mysql";
const dbhost="localhost";
const dbname="root";
const dbpassword="123456";
const db="z_gov";
const dbcharset="gb2312";
const sitename="延边自治州公安局网络监察处";
private $city=array("支队","龙井","和龙","图们","安图","汪清","敦化","珲春");
private $lv=array("一级","二级","三级");
protected $PDO=null;
static $instance=null;
protected function __construct()
{
$dns=self::dbtype.":host=".self::dbhost.";dbname=".self::db.";charset=".self::dbcharset;
$this->;PDO=new PDO($dns,self::dbname,self::dbpassword);
$this->;PDO->query("set names gb2312");
}
public static function getInstance()
{
if(self::$instance == null)
{
self::$instance = new Config();
}
return self::$instance;
}
public function getPDO()
{
return $this->;PDO;
}
public function showMessage($messid)
{
header("Location:error.php?messid=".$messid);
}
public function Execute($sql)
{
$this->;PDO->exec($sql);
}
public function showCity($selectedStr="支队")
{
foreach($this->city as $val)
{
if($val==$selectedStr)
{
echo "<option selected=\"selected\" value=\"$val\">$val</option>\r\n";
}else {
echo "<option value=\"$val\">$val</option>\r\n";
}
}
}
public function showLevel($selectedStr="一级")
{
foreach($this->lv as $val)
{
if($val==$selectedStr)
{
echo "<option selected=\"selected\" value=\"$val\">$val</option>\r\n";
}else {
echo "<option value=\"$val\">$val</option>\r\n";
}
}
}
}
?>
PHP代码:
<?php
require_once("config.class.php");
/**
* T.T.R无限分类生成类
*请自行建立以下字段:classid parentid classname
*表明请自行建立并修改相应常量,
*/
class TTR_Class extends Config
{
const classtable="class";
const tag_id="<!--{{id}}-->";
const tag_name="<!--{{name}}-->";
private $reptag=array(
"tag_spanceimg"=>"<img src=\"image/tree_line4.gif\">",
"tag_haschildimg"=>"<img src=\"image/tree_folder4.gif\">",
"tag_nochildimg"=>"<img src=\"image/tree_folder3.gif\">",
"tag_endchildimg"=>"<img src=\"image/tree_line2.gif\">",
"tag_childimg"=>"<img src=\"image/tree_line1.gif\">",
"tag_childline"=>"<img src=\"image/tree_line3.gif\">"
);
private $tag=array();
static $instance=null;
private $classarray=array();
private $newclassarray=array();
private $keyarr=array();
private $delidarray=array();
private $parentidarray=array();
private $classstr=null;
private $template=null;
private $select=null;
private $menujs="m_js=new Array();\n";
protected function __construct()
{
parent::__construct();
$this->tag=array(self::tag_id ,self::tag_name );
}
public static function getInstance()
{
if(self::$instance == null)
{
self::$instance = new TTR_Class();
}
return self::$instance;
}
public function addClass($classname,$parentid=0)
{
$sql="insert into ".self::classtable ."(parentid,classname) values('$parentid','$classname')";
$this->PDO->query($sql);
$this->showMessage(1);
}
public function delClass($classid)
{
if($this->hasChild($classid))
{
$this->delidarray[]=$classid;
$this->getAllChild($classid);
foreach ($this->delidarray as $val)
{
$sql="delete from ".self::classtable ." where classid='$val'";
$this->PDO->query($sql);
}
}else {
$sql="delete from ".self::classtable ." where classid='$classid'";
$this->PDO->query($sql);
}
$this->showMessage(2);
}
public function editClass($classid,$classname)
{
$sql="update ".self::classtable." set classname='$classname' where classid='$classid'";
$this->PDO->query($sql);
$this->showMessage(3);
}
private function getAllChild($classid)
{
//if(in_array($classid,$this->keyarr))
//{
foreach ($this->classarray[$classid] as $val)
{
$this->delidarray[]=$val[0];
if($this->hasChild($val[0])) $this->getAllChild($val[0]);
}
//}
}
private function setClassArray()
{
$sql="select * from ".self::classtable ." order by parentid,classid";
$rs=$this->PDO->query($sql);
$rs->setFetchMode(PDO::FETCH_ASSOC);
$arr=$rs->fetchAll();
foreach ($arr as $val)
{
$this->classarray[$val["parentid"]][]=array($val["classid"],$val["classname"]);
}
foreach($this->classarray as $key=>$val)
{
$this->keyarr[]=$key;
}
}
public function Build($str="")
{
$this->setClassArray();
$this->setTemplate($str);
}
//生成树状菜单
public function createTree($n=0,$lv=0)
{
foreach($this->classarray[$n] as $key=>$val)
{
if($n==0)
{
if($this->hasChild($val[0]))
{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_haschildimg"]."<b>".$val[1]."</b>"),$this->template);
}else{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_nochildimg"]."<b>".$val[1]."</b>"),$this->template);
}
}else{
$temparray=end($this->classarray[$n]);
if($temparray[0]==$val[0])
{
if($this->hasChild($val[0]))
{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_endchildimg"].$this->reptag["tag_haschildimg"].$val[1]),$this->template);
}else{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_endchildimg"].$this->reptag["tag_nochildimg"].$val[1]),$this->template);
}
}else{
if($this->hasChild($val[0]))
{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_childimg"].$this->reptag["tag_haschildimg"].$val[1]),$this->template);
}else{
$this->classstr.=str_replace($this->tag,array($val[0],$this->spanceImage($lv,$val[0]).$this->reptag["tag_childimg"].$this->reptag["tag_nochildimg"].$val[1]),$this->template);
}
}
}
if(in_array($val[0],$this->keyarr)) $this->createTree($val[0],$lv+1);
}
}
//生成下拉菜单
public function createSelect($selectedid=0,$n=0,$lv=0)
{
foreach($this->classarray[$n] as $key=>$val)
{
//echo $selectedid."=>".$val[0].",";
//echo $val[0]."->".$selectedid."=>".$selected;
if($selectedid==$val[0])
{
$selected="selected=\"seleceted\"";
}
if($n==0)
{
$this->select.="<option value=\"$val[0]\" $selected>$val[1]</option>";
}else{
$temparray=end($this->classarray[$n]);
if($temparray[0]==$val[0])
{
$this->select.="<option value=\"$val[0]\" $selected> ".$this->setSelectSpance($lv,$val[0])."└ $val[1]</option>";
}else{
$this->select.="<option value=\"$val[0]\" $selected> ".$this->setSelectSpance($lv,$val[0])."├ $val[1]</option>";
}
}
$selected=null;
if(in_array($val[0],$this->keyarr)) $this->createSelect($selectedid,$val[0],$lv+=1);
}
}
private function setSelectSpance($n,$id)
{
$lv=$id;
$idarr=array();
$str=null;
for($i=0;$i<$n;$i++)
{
$lv=$this->getParentId($lv);
$idarr[]=$lv;
}
$idarr=array_reverse($idarr);
foreach($idarr as $val)
{
$upclass=$this->getParentId($val);
@$tempid=end($this->classarray[$upclass]);
if($upclass!=0)
{
if($tempid[0]==$val)
{
$str.=" ";
}else{
$str.="│ ";
}
}
}
return $str;
}
private function hasChild($key)
{
return in_array($key,$this->keyarr);
}
private function getParentId($id)
{
$reid=null;
foreach($this->keyarr as $v)
{
foreach($this->classarray[$v] as $key=>$val)
{
if($val[0]==$id)
{
$reid=$v;
}
}
}
return $reid;
}
private function spanceImage($n,$id)
{
$lv=$id;
$idarr=array();
$str=null;
for($i=0;$i<$n;$i++)
{
$lv=$this->getParentId($lv);
//$str.=$str.=$this->reptag["tag_childline"];
$idarr[]=$lv;
}
$idarr=array_reverse($idarr);
foreach($idarr as $val)
{
$upclass=$this->getParentId($val);
$tempid=end($this->classarray[$upclass]);
if($upclass!=0)
{
if($tempid[0]==$val)
{
$str.=$this->reptag["tag_spanceimg"];
}else{
$str.=$this->reptag["tag_childline"];
}
}
}
return $str;
//return implode("",$idarr);
}
private function setTemplate($str)
{
$this->template=$str;
}
public function getClassStr()
{
return $this->classstr;
}
public function getSelect()
{
return $this->select;
}
public function showBigClass()
{
foreach ($this->classarray[0] as $val)
{
echo "<td id=\"a_$val[0]\" width=\"80\"> <a href=\"class.php?classid=".$val[0]."\" onMouseOver=\"showDiv('$val[0]','".$this->getMenuStr($val[0])."')\">".$val[1]."</a></td>";
}
}
public function getClassArray()
{
return $this->classarray;
}
public function getClassChild($classid)
{
$this->delidarray=null;
if($this->hasChild($classid))
{
$this->delidarray[]=$classid;
$this->getAllChild($classid);
}else{
$this->delidarray[]=$classid;
}
return $this->delidarray;
}
private function setParentPathArray($classid)
{
if($this->getParentId($classid)!=0)
{
$this->parentidarray[]=$this->getParentId($classid);
$this->setParentPathArray($this->getParentId($classid));
}
}
public function getClassName($classid)
{
foreach ($this->classarray[$this->getParentId($classid)] as $val)
{
if($val[0]==$classid)
{
$str=$val[1];
break;
}
}
return $str;
}
public function getParentPathString($classid)
{
$this->parentidarray[]=$classid;
$this->setParentPathArray($classid);
$this->parentidarray=array_reverse($this->parentidarray);
foreach ($this->parentidarray as $val)
{
$str.="<a href=\"class.php?classid=".$val."\">".$this->getClassName($val)."</a>>";
}
return "<a href=\"view.php\">首页</a>>".substr($str,0,strlen($str)-1);
}
private function getMenuStr($classid)
{
foreach ($this->classarray[0] as $arr)
{
if($arr[0]!=$classid)
{
$a[]=$arr[0];
}
}
return implode("|",$a);
}
private function createJsArray($n,$jsarrname="m_js")
{
foreach ($this->classarray[$n] as $arr)
{
$this->menujs.=$jsarrname."[".$arr[0]."]=new Array(\"".$arr[0]."\",\"".$arr[1]."\");\n";
if(in_array($arr[0],$this->keyarr)) $this->createJsArray($arr[0],$jsarrname."[".$arr[0]."]");
}
}
//取得JS无限分类数组
public function getJsArray()
{
$this->createJsArray(0);
return $this->menujs;
}
public function showClassDiv()
{
foreach ($this->classarray[0] as $arr)
{
echo "<div id=\"$arr[0]\">\n";
echo "<script language=\"javascript\">\n";
echo "showD($arr[0],\"$arr[0]\");\n";
echo "CheckClass(m_js[$arr[0]],\"$arr[0]_\");\n";
echo "</script>\n";
echo "</div>\r\r\n";
}
}
public function getRootClassId()
{
foreach ($this->classarray[0] as $arr)
{
$a[]=$arr[0];
}
return implode("|",$a);
}
}
?>
使用方法:复制PHP内容到剪贴板
PHP代码:
<?php
$str="<tr><td><!--{{id}}--></td><td><!--{{name}}--></td></tr>";
$a=TTR_Class::getInstance($str);
?>
<table width="300" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="40" height="30">编号</td><td>栏目名称</td>
</tr>
<?php echo $a->getClassStr();?>
</table>
http://www.phpchina.com/bbs/viewthread.php?tid=40313&extra=page%3D1%26amp%3Bfilter%3Ddigest%26amp%3Borderby%3Ddateline%26amp%3Bascdesc%3DDESC
Leave A Comment