您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

一个无限分类的处理类

2024/4/17 17:08:36发布3次查看
php代码:--------------------------------------------------------------------------------
分类id,
// 'parentid' => 父分类id,
// 'rootid' => 根分类id,
// 'categoryname' => 分类名称,
// ),
// ……
// );
// 表示的一颗树
//
// @param: $parentnode 父分类id, 每一次由调用者给出,递归时由程序计算传递
//
// return value: 返回以兄弟双亲法表示的所有分类的树
// 注意: 确保当前分类已经设置,否则此函数无返回
//
//---------------------------------------------------------------------------
function getnodedata($data, $parentnode)
{
$arr = array();
$arraycount = 0;
for($i = 0, $cnt = count($data); $i {
if($data[$i]['parentid'] == $parentnode)
{
$arr[$arraycount] = $data[$i];
$arr[$arraycount++]['child'] = $this->getnodedata($data, $data[$i]['id']);
}
}
return $arr;
}
//---------------------------------------------------------------------------
//private string _currentlevel(array $data, int $current, string $processfunc = '')
// 根据一颗指定根的并且以兄弟双亲法表示的树和当前分类的id,返回当前分类在整个分类表中所处的位置
//
// @param: $data 兄弟双亲法表示的树, 由调用者传递
//
// @param: $current 当前分类id,第一次调用时由调用者给出,递归时由程序自行计算
//
// @param: $processfunc 指定对分类数据的处理函数, 函数原型定义见 $this->printcurrentlevel 中的注释
//
// return value: 返回当前分类在分类树中的位置
// 注意: 确保当前分类已经设置,否则此函数无返回
//
//---------------------------------------------------------------------------
function _currentlevel($data, $current, $processfunc = '')
{
for($i = 0; $i {
if($data[$i]['id'] == $current)
{
if($data[$i]['parentid'] != 0)
{
$str = $this->_currentlevel($data, $data[$i]['parentid'], $processfunc) . ' -> ';
if($processfunc) $str .= $processfunc($data[$i]);
else $str .= $data[$i]['categoryname'];
}
else
{
if($processfunc) $str = $processfunc($data[$i]);
else $str = $data[$i]['categoryname'];
}
break;
}
}
return $str;
}
//---------------------------------------------------------------------------
//public category_logic(object &$kernel, int $categoryid = -1)
// 本类构造函数
//
// @param: $kernel 此参数为当前系统核心类的一个引用, 核心类中包括
// 数据库类、输入输出类、系统配置类等
//
// @param: $categoryid 当前分类id。
// 当想调用 printcurrentlevel、getrootid、getparentid、generatetypetreelist及
// 调用_currentitem成员的方法时请先设置此值.
//
// 调用generatetypetreelist时设置此值,则没有id为此的分类默认被选择,没设置则无默认
//
// return value: none
//
//---------------------------------------------------------------------------
function &category_logic(&$kernel, $categoryid = -1)
{
$this->kernelref = &$kernel;
$this->tblobj = new table($kernel->dbobj, dbtable_category);
if($categoryid != -1)
{
$this->setcategoryid($categoryid);
}
}
//---------------------------------------------------------------------------
//public void setcategoryid(int $categoryid)
// 设置当前分类id
//
// return value: none
//
//---------------------------------------------------------------------------
function setcategoryid($categoryid)
{
if(!$categoryid) return;
$item = new titem($this->kernelref->dbobj, dbtable_category, '*', $categoryid ,'id');
$this->_selfdata = &$item;
$this->categoryid = $categoryid;
}
//---------------------------------------------------------------------------
//public int getrootid()
// 返回当前分类的根分类id
// 注意:只有设置的当前分类时此函数才有效
//
// return value: 返回当前分类的根分类id
//
//---------------------------------------------------------------------------
function getrootid()
{
return $this->_selfdata->get('rootid');
}
//---------------------------------------------------------------------------
//public int getparentid()
// 返回当前分类的父分类id
// 注意:只有设置的当前分类时此函数才有效
//
// return value: 返回当前分类的父分类id
//
//---------------------------------------------------------------------------
function getparentid()
{
if($this->categoryid) return $this->_selfdata->get('parentid');
}
//---------------------------------------------------------------------------
//public string generatetypetreelist(array $data, string $processfunc, int $floor = 0)
// 返回整个分类的树状结构放在optionlist中的列表
//
// @param: $data 此参数由 $this->dumptypedatatotree() 返回
//
// @param: $processfunc 处理显示分类信息的回调函数, 函数原型请参照: $this->printcurrentlevel()
//
// @param: $floor 本参数不能人为给出,是程序自动计算的中间值
//
// return value:
// 结构为一颗兄弟双亲表示法表示的树
// 设如分类数据如下:
// ├──1级分类
// │
// │
// │
// ├─2级分类
// │ │
// │ └─3级分类
// │
// └─2级分类
//
// 则返回值为 array(
// 0 => array(
// 'id' => '',
// 'parentid' => '',
// 'rootid' => '',
// 'categoryname' => '',
// 'child' => ....
// )
// .....
// )
//
//---------------------------------------------------------------------------
function dumptypedatatotree($rootid = 0, $fields = '*')
{
$this->tblobj->setfields($fields);
$this->tblobj->setcondition('');
$list = $this->tblobj->mapresult($this->tblobj->select());
return $this->getnodedata($list, $rootid);
}
//---------------------------------------------------------------------------
//public string generatetypetreelist(array $data, string $processfunc = '', int $floor = 0)
// 返回整个分类的树状结构放在optionlist中的列表
//
// @param: $data 此参数由 $this->dumptypedatatotree() 返回
//
// @param: $processfunc 处理显示分类信息的回调函数, 函数原型请参照: $this->printcurrentlevel()
//
// @param: $floor 本参数不能人为给出,是程序自动计算的中间值
//
// return value: 返回一个分类名称1 ... 分类名称n
//
// ps: 调用时echo . $_c->generatetypetreelist($data, 'processfunc') . ;
//
//---------------------------------------------------------------------------
function generatetypetreelist($data, $processfunc, $floor = 0)
{
$str = '';
for($i = 0, $cnt = count($data); $i {
if($this->categoryid == $data[$i]['id'])
{
$str .=
. str_repeat( , $floor * 3)
. '├'
. ($processfunc ? $processfunc($data[$i]) : $data[$i]['categoryname'])
. \n;
}
else
{
$str .=
. str_repeat( , $floor * 3)
. '├'
. ($processfunc ? $processfunc($data[$i]) : $data[$i]['categoryname'])
. \n;
}
if($data[$i]['child']) $str .= $this->generatetypetreelist($data[$i]['child'], $processfunc, $floor + 1);
}
return $str;
}
//---------------------------------------------------------------------------
//public string generatetypetreeview(array $data, string $processfunc = '')
// 返回整个分类的树状结构视图
//
// @param: $data 此参数由 $this->dumptypedatatotree() 返回
//
// @param: $processfunc 处理显示分类信息的回调函数, 函数原型请参照: $this->printcurrentlevel()
//
// return value: 返回生成的一颗html形式显示的树
//
//---------------------------------------------------------------------------
function generatetypetreeview($data, $processfunc)
{
$str = '
';
for($i = 0, $cnt = count($data); $i {
if($processfunc) $str .= '' . $processfunc($data[$i]) . '' . \n;
else $str .= '' . $data[$i]['categoryname'] . '' . \n;
if($data[$i]['child']) $str .= '' . $this->generatetypetreeview($data[$i]['child'], $processfunc) . '';
}
$str .= '';
return $str;
}
//---------------------------------------------------------------------------
//public string printcurrentlevel(string $processfunc = '')
// 对多级分类生成当前位置字符串
// 设如分类数据如下,当前分类为3级分类, 则调用返回 1级分类 -> 2级分类 -> 3级分类
// ├──1级分类
// │
// │
// │
// ├─2级分类
// │ │
// │ └─3级分类
// │
// └─2级分类
//
//
//
//
// @param: $processfunc 此为对分类数据如何显示的回调函数,不设置则直接显示分类名称
// 函数定义原型为 function (&$arr);
// 其中$arr参数为每一个分类信息的一维数组如下:
// array(id => 1, parentid => 0, rootid => 0, categoryname => '1级分类')
// 返回值为对上述数据处理的结果,比如返回带链接的分类名字、更改显示颜色等
//
// return value: 返回当前分类在整个分类树中所处位置
//
//---------------------------------------------------------------------------
function printcurrentlevel($processfunc = '')
{
if(!$this->categoryid) return '';
if($this->_selfdata->get(rootid) == 0)
{
if($processfunc) return $processfunc($this->_selfdata->fetchdatatoarray());
else return $this->_selfdata->get(categoryname);
}
$current = $this->categoryid;
$this->tblobj->setcondition('rootid = ' . $this->_selfdata->get('rootid') . or id = . $this->_selfdata->get('rootid'));
$data = $this->tblobj->mapresult($this->tblobj->select());
return $this->_currentlevel($data, $current, $processfunc);
}
//---------------------------------------------------------------------------
//public boolean add(array $arr)
// 添加新分类到分类表中
//
// @param: $arr 在此数组中包括对新添加分类的定义, 定义如下:
//
// $arr['rootid'] 新分类所属的根分类id
// $arr['parentid'] 新分类的父分类id
// $arr['categoryname'] 新分类的名称
//
// return value: 返回添加分类操作结果
//
//---------------------------------------------------------------------------
function add($arr)
{
$this->tblobj->setfields(
array(
'rootid',
'parentid',
'categoryname',
)
);
return $this->tblobj->insert(
array(
$arr['rootid'],
$arr['parentid'],
$arr['categoryname'],
)
);
}
//---------------------------------------------------------------------------
//public boolean delete(int $id)
// 删除已经存在的分类
//
// @param: $id 要删除的分类id
//
// return value: 返回删除分类操作结果
//
//---------------------------------------------------------------------------
function delete($id)
{
$sysoption = &$this->kernelref->config;
$this->tblobj->setfields('*');
$this->tblobj->setcondition('id = ' . (int)$id);
return $this->tblobj->delete();
}
//---------------------------------------------------------------------------
//public boolean modify(int $id, array $arr)
// 修改已经存在的分类
//
// @param: $id 要修改的分类id
// @param: $arr 在此数组中包括修改后的分类定义, 定义如下:
//
// $arr['rootid'] 新分类所属的根分类id
// $arr['parentid'] 新分类的父分类id
// $arr['categoryname'] 新分类的名称
//
// return value: 返回修改分类操作结果
//
//---------------------------------------------------------------------------
function modify($id, $arr)
{
$this->tblobj->setcondition('id = ' . (int)$id);
$prev = $this->tblobj->maponerow($this->tblobj->select());
$this->tblobj->setfields(
array(
'rootid',
'parentid',
'categoryname',
)
);
return $this->tblobj->update($arr);
}
//---------------------------------------------------------------------------
//public array modify(int $id)
// 修改已经存在的分类
//
// @param: $id 指定的分类id
//
// return value: 返回指定id分类的信息
// 数组中包括:
// array(
// 'id' => 分类id,
// 'parentid' => 父分类id,
// 'rootid' => 根分类id,
// 'categoryname' => 分类名称,
// );
//
//---------------------------------------------------------------------------
function getcategory($id)
{
$this->tblobj->setcondition('id = ' . (int)$id);
return $this->tblobj->maponerow($this->tblobj->select());
}
}
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录