求唠叨写的无限分类树 是做广告吗?无限分类树,可以google一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php//无限分类类class sortClass{ /* getTree();返回排序好的节点数组,每个节点的深度已经整理好了 getList($root,$self);//获取指定节点下面的子节点,$root指节点id,self指是否包括自身节点id,0为不包括,默认为1 getList(0,0)获取所有节点,不包括“顶级节点”,因为顶级节点没有存放到lineRs数组当中去,一般也用不到 其它情况正常使用 getPosition($id,$self=1);返回当前位置,默认包括自己 getRootFid($id)//获取当前分类的最顶级分类id */ public $child = array(); public $parent = array(); public $name = array(); public $lineRs = array();//行记录 function __construct($sql)//构造函数 { global $db; $this->setNode(0, -1, '顶极节点'); $result = $db->query($sql); foreach($result as $val) { $this->lineRs[$val["id"]] = $val; $this->setNode($val["id"],$val["fid"]); } $this->initDepth(); } function getTree() { return $this->lineRs;//构造函数返回初始化好的节点数组 } function setNode($id,$parent) { $this->child[$parent][] = $id; $this->parent[$id] = $parent; } function initDepth($root=0,$depth=0)//初始化节点深度 { if (!isset($this->child[$root])) return; foreach ($this->child[$root] as $key=>$id) { $this->lineRs[$id]["depth"] = $depth; if (isset($this->child[$id])) { if ($this->child[$id]) $this->initDepth($id,$depth+1); } } } function getList($root=0,$self=1) { $tree = array(); if($self == 1) { $tree[] = $root; } $this->getList_1($root,$tree); return $tree; } function getList_1($root,&$tree) { if (!isset($this->child[$root])) return; foreach ($this->child[$root] as $key=>$id) { $tree[] = $id; if (isset($this->child[$id])) { $this->getList_1($id,$tree); } } } function getPosition($id,$self=1) { $position = array(); if($self == 1) { $position[] = $id; } while($this->parent[$id] != 0) { $id = $this->parent[$id]; $position[] = $id; } //return implode(array_reverse($position)," → "); return array_reverse($position); } function getRootFid($id) { while($this->parent[$id] != 0) { $id = $this->parent[$id]; } return $id; }}//使用方法//include("include.php");//$sql = "select * from aa order by orderId asc,id asc";//$sort = new sortClass($sql);//$tree = $sort->getTree();//$childes = $sort->getList(0,0);//foreach($childes as $key=>$id)//{// echo str_repeat("-",$tree[$id]["depth"]) . $tree[$id]["name"] . "<br>";//}//echo "<hr>";//$postion = $sort->getPosition(5);//foreach($postion as $key=>$id)//{// echo $tree[$id]["name"] . " -> ";//}?> 想要用php链接sqlite保存网页信息 求助 150页的数据,我想分成每10页一采。我应该怎么操作 php 页面能正常访问,但是一刷新就成空白页面了 php文件可以在服务器启动时自动执行吗? 菜鸟求助:简单的留言簿,提交留言的时候显示下载页面,不知为什么? 问一个很简单的PHP问题 PHP 在 字符串数组中查找字字符串 如何实现页面中的中英文切换? 数组问题 这个怎么做呢 做采集的时候遇到的难题,请大家帮忙。 急求一sql
//无限分类类
class sortClass
{
/*
getTree();返回排序好的节点数组,每个节点的深度已经整理好了
getList($root,$self);//获取指定节点下面的子节点,$root指节点id,self指是否包括自身节点id,0为不包括,默认为1
getList(0,0)获取所有节点,不包括“顶级节点”,因为顶级节点没有存放到lineRs数组当中去,一般也用不到
其它情况正常使用
getPosition($id,$self=1);返回当前位置,默认包括自己
getRootFid($id)//获取当前分类的最顶级分类id
*/
public $child = array();
public $parent = array();
public $name = array();
public $lineRs = array();//行记录
function __construct($sql)//构造函数
{
global $db;
$this->setNode(0, -1, '顶极节点');
$result = $db->query($sql);
foreach($result as $val)
{
$this->lineRs[$val["id"]] = $val;
$this->setNode($val["id"],$val["fid"]);
}
$this->initDepth();
}
function getTree()
{
return $this->lineRs;//构造函数返回初始化好的节点数组
}
function setNode($id,$parent)
{
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
}
function initDepth($root=0,$depth=0)//初始化节点深度
{
if (!isset($this->child[$root])) return;
foreach ($this->child[$root] as $key=>$id)
{
$this->lineRs[$id]["depth"] = $depth;
if (isset($this->child[$id]))
{
if ($this->child[$id]) $this->initDepth($id,$depth+1);
}
}
}
function getList($root=0,$self=1)
{
$tree = array();
if($self == 1)
{
$tree[] = $root;
}
$this->getList_1($root,$tree);
return $tree;
}
function getList_1($root,&$tree)
{
if (!isset($this->child[$root])) return;
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if (isset($this->child[$id]))
{
$this->getList_1($id,$tree);
}
}
}
function getPosition($id,$self=1)
{
$position = array();
if($self == 1)
{
$position[] = $id;
}
while($this->parent[$id] != 0)
{
$id = $this->parent[$id];
$position[] = $id;
}
//return implode(array_reverse($position)," → ");
return array_reverse($position);
}
function getRootFid($id)
{
while($this->parent[$id] != 0)
{
$id = $this->parent[$id];
}
return $id;
}
}
//使用方法
//include("include.php");
//$sql = "select * from aa order by orderId asc,id asc";
//$sort = new sortClass($sql);
//$tree = $sort->getTree();
//$childes = $sort->getList(0,0);
//foreach($childes as $key=>$id)
//{
// echo str_repeat("-",$tree[$id]["depth"]) . $tree[$id]["name"] . "<br>";
//}
//echo "<hr>";
//$postion = $sort->getPosition(5);
//foreach($postion as $key=>$id)
//{
// echo $tree[$id]["name"] . " -> ";
//}
?>