三级分类 调用方法

<?php
class Goods {
var $db;

public function __construct($db){
$this->db = $db;
}

/**
* 获得指定分类同级的所有分类以及该分类下的子分类
*
* @access public
* @param integer $cat_id 分类编号
* @return array
*/
function get_categories_tree()
{

/*
判断当前分类中全是是否是底级分类,
如果是取出底级分类上级分类,
如果不是取当前分类及其下的子分类
*/

/* 获取当前分类及其子分类 */
$sql = 'SELECT tID,tname ,two_id,is_show ' .
" from ntc_type where two_id = 0 ORDER BY torder ASC LIMIT 13";
$res = $this->db->select($sql);

foreach ($res AS $k=>$row)
{
if ($row->is_show)
{
$cat_arr[$k]['id'] = $row->tID;
$cat_arr[$k]['name'] = $row->tname;
$cat_arr[$k]['url'] = "index.php?module=shop&action=Category&id=$row->tID";
// $cat_arr[$k]['zdy'] = "123";
$getSon = $this->get_child_tree($row->tID,1);
if ($getSon)
{
$cat_arr[$k]['son'] = $this->get_child_tree($row->tID);
}
}
}
if(isset($cat_arr))
{
return $cat_arr;
}
}

function get_child_tree($tree_id,$get=0)
{
$three_arr = array();

$sql = "SELECT tID, tname, two_id, is_show from ntc_type where two_id = '$tree_id' ORDER BY torder ASC";
$res = $this->db->select($sql);
if($get && empty($res)){
return false;
}
foreach ($res AS $k=>$row)
{
if ($row->is_show)

$three_arr[$k]['id'] = $row->tID;
$three_arr[$k]['name'] = $row->tname;
$three_arr[$k]['url'] = "index.php?module=shop&action=Category&id=$row->tID";

$getSon = $this->get_child_tree($row->tID,1);
if ($getSon)
{
$three_arr[$k]['sunzi'] = $this->get_child_tree($row->tID);

}
}
return $three_arr;
}

 


//自定义页面调用
require_once(MO_LIB_DIR . '/Goods.class.php');

/*商品分类*/
$show = new Goods($db);
$category = $show->get_categories_tree();

 

 


?>

posted @ 2015-09-16 15:56  流年沉默的如此苍凉╰╮  阅读(402)  评论(0编辑  收藏  举报