欢迎光临
我们一直在努力

一个无限分类的处理类-PHP教程,PHP应用

建站超值云服务器,限时71元/月

php代码:——————————————————————————–

<?php

/* 名称: 对分类操作的业务逻辑封装

*

* 作者: 帅的像人渣 qq: 1191391 e-mail: netcat2@21cn.com

*

* 完成日期: 2003-12-18 13:33

*

* 说明: 本类中引用的其它类(db、table、item)均未提供,所以本类只能做个参考,不能直接应用

* 不是本人小气不提供其它类,实在是因为那些都是一两年前写的类,很烂。怕大家看后对大

* 造成误导. 在此发表这个类,只希望大家能从中学到一些程序设计的方法。

* 授人以鱼不如授人以渔~

*

* 特点:

* 采用递归调用的方法,对分类数据只需一次数据库查询可生成树状结构。 无限递归层次(视机器堆栈而定)

*

* 数据库定义:

* id smallint unsigned primary #如果数据量很大可用int

* parentid smallint unsigned index #如果数据量很大可用int, 请索引此字段

* #如果为根分类,则parentid = 0

*

* rootid smallint unsigned index #如果数据量很大可用int, 请索引此字段

* #如果是根分类则rootid = 0, 否则rootid = 最上层的父分类id

* categoryname varchar(n) #此大小自定

* 如需有其它字段定义附在后面

* 注意事项:

* 不要试图直接调用本类,除非你有和我定义那另外那几个类相对应的接口, 否则不会成功

* 在合适的地方定义 dbtable_category 这个常量,使其指向你的分类数据表名字

*

* 程序构架:

* ├─基础类 <!– 完成底层数据库操作、数据抽象、语言、模板、异常、杂项等)操作 –>

* │

* │

* └─业务逻辑层(此类所处层次) <!– 利用基础类中数据操作、数据抽象等类根据表现层传递的参数完成数据处理,并返回数据或操作结果 –>

* │

* │

* └───表现层(用户界面) <!– 利用业务逻辑层将取得的数据或操作数据的结果通过基础类中的界面等类进行显示 –>

*/

define(dbtable_category, xxx);

class category_logic

{

var $kernelref = null; //系统核心的引用

var $tblobj = null; //包含当前分类数据 table 类的实例

var $_currentitem = null; //包含当前分类数据 titem类的实例

var $categoryid = 0; //当前分类id,如果没有当前分类此项为 0

//—————————————————————————

//private array getnodedata(array $data, int $parentnode)

// 根据一颗指定根的并且以兄弟双亲法表示的树和当前分类的id,返回当前分类在整个分类表中所处的位置

//

// @param: $data 2维数组 array(

// array(

// id => 分类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 < $cnt; $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 < count($data); $i++)

{

if($data[$i][id] == $current)

{

if($data[$i][parentid] != 0)

{

$str = $this->_currentlevel($data, $data[$i][parentid], $processfunc) . -&gt; ;

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: 返回一个<option>分类名称1</option> … <option>分类名称n</option>

//

// ps: 调用时echo "<select name=xxxx>" . $_c->generatetypetreelist($data, processfunc) . "</select>";

//

//—————————————————————————

function generatetypetreelist($data, $processfunc, $floor = 0)

{

$str = ;

for($i = 0, $cnt = count($data); $i < $cnt; $i++)

{

if($this->categoryid == $data[$i][id])

{

$str .= "<option value={$data[$i][id]} selected>"

. str_repeat("&nbsp;", $floor * 3)

. ├

. ($processfunc ? $processfunc($data[$i]) : $data[$i][categoryname])

. "</option>\n";

}

else

{

$str .= "<option value={$data[$i][id]}>"

. str_repeat("&nbsp;", $floor * 3)

. ├

. ($processfunc ? $processfunc($data[$i]) : $data[$i][categoryname])

. "</option>\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 = <ul style="line-height:200%">;

for($i = 0, $cnt = count($data); $i < $cnt; $i++)

{

if($processfunc) $str .= <li> . $processfunc($data[$i]) . </li> . "\n";

else $str .= <li> . $data[$i][categoryname] . </li> . "\n";

if($data[$i][child]) $str .= <li> . $this->generatetypetreeview($data[$i][child], $processfunc) . </li>;

}

$str .= </ul>;

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());

}

}

?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个无限分类的处理类-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章