zen cart 类:category_tree

zen cart 类 category_tree 位于 /includes/classes/category_tree.php 文件中,主要的作用是,产生商品分类的树状目录。

  1. <?php  
  2. /**  
  3.  * category_tree Class.  
  4.  *  
  5.  * @package classes  
  6.  * @copyright Copyright 2003-2006 Zen Cart Development Team  
  7.  * @copyright Portions Copyright 2003 osCommerce  
  8.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0  
  9.  * @version $Id: category_tree.php 3041 2006-02-15 21:56:45Z wilt $  
  10.  */   
  11. if  (!defined( 'IS_ADMIN_FLAG' )) {  //防止非法访问   
  12.   die ( 'Illegal Access' );  
  13. }  
  14. /**  
  15.  * category_tree Class.  
  16.  * This class is used to generate the category tree used for the categories sidebox  
  17.  *  
  18.  * @package classes  
  19.  */   
  20.  // 继承 base 基本类   
  21. class  category_tree  extends  base {  
  22.      // zen_category_tree 方法,传递一个参数:$product_type,默认值是:all   
  23.   function  zen_category_tree( $product_type  =  "all" ) {  
  24.     global   $db$cPath$cPath_array// 定义3个 global 变量。$cPath 和 $cPath_array 可以在 /includes/init_includes/init_category_path.php 文件中找到   
  25.     // 如果 $product_type 不是 'all',则执行以下代码(因为默认值是 all,所以如果不传递任何参数,则以下代码不会被执行)   
  26.     if  ( $product_type  !=  'all' ) {   
  27.       // 将一条 sql 语句赋值给 $sql 变量。该语句的作用是   
  28.         // 从 product_types 数据库表中选择 type_master_type 字段,必须是和 $product_type 相等的 type_master_type 字段   
  29.       $sql  =  "select type_master_type from "  . TABLE_PRODUCT_TYPES . "  
  30.                 where type_master_type = " . $product_type . " ";  
  31.       // 执行 $sql 语句   
  32.       $master_type_result  =  $db ->Execute( $sql );  
  33.       // 从 $master_type_result 对象中得到 type_master_type 字段值   
  34.       $master_type  =  $master_type_result ->fields[ 'type_master_type' ];  
  35.     }  
  36.       
  37.     $this ->tree =  array ();  // 定义一个类成员 tree 为数组,$this->tree 是类变量的表示方法,如果不在类里面定义,其实就是 $tree   
  38.     if  ( $product_type  ==  'all' ) {  // 如果 $product_type 的值等于 all   
  39.       // 将一条 sql 语句赋值给 $categories_query   
  40.        // 该语句的作用是多表查询 categories 和 categories_description 数据库表   
  41.         // 返回 categories 表中的 categories_id,parent_id,categories_image 字段;categories_description 表中的 categories_name 字段   
  42.         // 返回结果必须符合:parent_id = 0,同一个 categories_id,language_id = $_SESSION['languages_id'] 以及 categories_status= 1   
  43.         // 返回结果按照 categories_name 排序   
  44.       $categories_query  = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image  
  45.                              from " . TABLE_CATEGORIES . "  c,  " . TABLE_CATEGORIES_DESCRIPTION . "  cd  
  46.                              where c.parent_id = 0  
  47.                              and  c.categories_id = cd.categories_id  
  48.                              and  cd.language_id= '" . (int)$_SESSION[' languages_id '] . "'   
  49.                              and  c.categories_status= 1  
  50.                              order by sort_order, cd.categories_name";  
  51.     } else  {  // 如果 $product_type 不为 all,则执行以下代码   
  52.       // 将一条 sql 语句赋值给 $categories_query,代码分析如上    
  53.       $categories_query  = "select ptc.category_id  as  categories_id, cd.categories_name, c.parent_id, c.categories_image  
  54.                              from " . TABLE_CATEGORIES . "  c,  " . TABLE_CATEGORIES_DESCRIPTION . "  cd,  " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "  ptc  
  55.                              where c.parent_id = 0  
  56.                              and  ptc.category_id = cd.categories_id  
  57.                              and  ptc.product_type_id =  " . $master_type . "   
  58.                              and  c.categories_id = ptc.category_id  
  59.                              and  cd.language_id= " . (int)$_SESSION['languages_id'] ."   
  60.                              and  c.categories_status= 1  
  61.                              order by sort_order, cd.categories_name";  
  62.     }  
  63.     // 执行 $categories_query 语句   
  64.     $categories  =  $db ->Execute( $categories_query'' , true, 150);  
  65.     while  (! $categories ->EOF)  {  // 查询结果没有出尽,则循环执行以下语句   
  66.       // 把查询得到的 categories_name,parent_id,categories_id,categories_image 和其它几个值合成一个数组,赋值给 $this->tree(X)   
  67.       // 这个 X 就是:$categories->fields['categories_id'] 即商品分类的 id   
  68.       $this ->tree[ $categories ->fields[ 'categories_id' ]] =  array (  
  69.                                                                                 'name'  =>  $categories ->fields[ 'categories_name' ],  
  70.                                                                                 'parent'  =>  $categories ->fields[ 'parent_id' ],  
  71.                                                                                 'level'  => 0,  
  72.                                                                                 'path'  =>  $categories ->fields[ 'categories_id' ],  
  73.                                                                                 'image'  =>  $categories ->fields[ 'categories_image' ],  
  74.                                                                                 'next_id'  => false  
  75.                                                                             );  
  76.       if  (isset( $parent_id )) {   // 如果存在 $parent_id 这个变量   
  77.         // 把查询得到的 categories_id 赋值给 $this->tree[X]['next_id'],在上面的代码中,next_id 这个值默认是 false   
  78.         $this ->tree[ $parent_id ][ 'next_id' ] =  $categories ->fields[ 'categories_id' ];  
  79.       }  
  80.       // 把查询得到的 categories_id 赋值给 $parent_id;   
  81.       $parent_id  =  $categories ->fields[ 'categories_id' ];  
  82.       if  (!isset( $first_element )) {  // 如果不存在 $first_element 这个变量   
  83.         // 将查询得到的 categories_id 赋值给 $first_element   
  84.         $first_element  =  $categories ->fields[ 'categories_id' ];  
  85.       }  
  86.       $categories ->MoveNext();  // $categories 往后移动一个指针   
  87.     }  
  88.       
  89.     if  (zen_not_null( $cPath )) {  // 如果 $cPath 不为空   
  90.       $new_path  =  '' ;   
  91.       reset($cPath_array );  // 把 $cPath_array 数组的内部指针指向第一个元素,并返回这个元素的值   
  92.       while  (list( $key$value ) = each( $cPath_array )) {  // 把 $cPath_array 元素为 $key $value 变量赋值   
  93.         unset($parent_id );  // 销毁 $parent_id 变量   
  94.         unset($first_id );  // 销毁 $first_id 变量   
  95.         if  ( $product_type  ==  'all' ) {  // 如果 $product_type 的值等于 all   
  96.           // sql 查询语句   
  97.           $categories_query  = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image  
  98.                                from " . TABLE_CATEGORIES . "  c,  " . TABLE_CATEGORIES_DESCRIPTION . "  cd  
  99.                                where c.parent_id = " . (int)$value . "   
  100.                                and  c.categories_id = cd.categories_id  
  101.                                and  cd.language_id= " . (int)$_SESSION['languages_id'] . "   
  102.                                and  c.categories_status= 1  
  103.                                order by sort_order, cd.categories_name";  
  104.         } else  {  // 如果 $product_type 的值不为 all   
  105.           /*  
  106.           $categories_query = "select ptc.category_id as categories, cd.categories_name, c.parent_id, c.categories_image  
  107.           from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc  
  108.           where c.parent_id = '" . (int)$value . "'  
  109.           and ptc.category_id = cd.categories_id  
  110.           and ptc.product_type_id = '" . $master_type . "'  
  111.           and cd.language_id='" . (int)$_SESSION['languages_id'] . "'  
  112.           and c.categories_status= '1'  
  113.           order by sort_order, cd.categories_name";  
  114.           */   
  115.           // sql 查询语句,可以和上一条比较,看有什么不同   
  116.           $categories_query  = "select ptc.category_id  as  categories_id, cd.categories_name, c.parent_id, c.categories_image  
  117.                              from " . TABLE_CATEGORIES . "  c,  " . TABLE_CATEGORIES_DESCRIPTION . "  cd,  " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "  ptc  
  118.                              where c.parent_id = " . (int)$value . "   
  119.                              and  ptc.category_id = cd.categories_id  
  120.                              and  ptc.product_type_id =  " . $master_type . "   
  121.                              and  c.categories_id = ptc.category_id  
  122.                              and  cd.language_id= " . (int)$_SESSION['languages_id'] ."   
  123.                              and  c.categories_status= 1  
  124.                              order by sort_order, cd.categories_name";  
  125.         }  
  126.         $rows  =  $db ->Execute( $categories_query );  // 执行 $categories_query 查询语句   
  127.         if  ( $rows ->RecordCount()>0) {  // 如果 $rows 对象中的 RecordCount() 方法返回的结果大于0   
  128.               
  129.           $new_path  .=  $value// 将 $cPath_array 数组的值,累计赋值给 $new_path   
  130.             
  131.           while  (! $rows ->EOF) {  // 如果 $rows 没出尽   
  132.               
  133.             $this ->tree[ $rows ->fields[ 'categories_id' ]] =  array (  
  134.                                                                                 'name'    =>  $rows ->fields[ 'categories_name' ],  
  135.                                                                                 'parent'   =>  $rows ->fields[ 'parent_id' ],  
  136.                                                                                 'level'      =>  $key +1,  
  137.                                                                                 'path'      =>  $new_path  .  '_'  .  $rows ->fields[ 'categories_id' ],  
  138.                                                                                 'image'    =>  $categories ->fields[ 'categories_image' ],  
  139.                                                                                 'next_id'  => false  
  140.                                                                             );  
  141.             if  (isset( $parent_id )) {  
  142.               $this ->tree[ $parent_id ][ 'next_id' ] =  $rows ->fields[ 'categories_id' ];  
  143.             }  
  144.             $parent_id  =  $rows ->fields[ 'categories_id' ];  
  145.             if  (!isset( $first_id )) {  
  146.               $first_id  =  $rows ->fields[ 'categories_id' ];  
  147.             }  
  148.             $last_id  =  $rows ->fields[ 'categories_id' ];  
  149.             $rows ->MoveNext();  
  150.           }  
  151.           $this ->tree[ $last_id ][ 'next_id' ] =  $this ->tree[ $value ][ 'next_id' ];  
  152.           $this ->tree[ $value ][ 'next_id' ] =  $first_id ;  
  153.           $new_path  .=  '_' ;  
  154.         } else  {  
  155.           break ;  
  156.         }  
  157.       }  
  158.     }  
  159.     $row  = 0;  
  160.     return   $this ->zen_show_category( $first_element$row );  
  161.   }  
  162.   function  zen_show_category( $counter , $ii ) {  
  163.     global   $cPath_array ;  
  164.     $this ->categories_string =  "" ;  
  165.     for  ( $i =0;  $i < $this ->tree[ $counter ][ 'level' ];  $i ++) {  
  166.       if  ( $this ->tree[ $counter ][ 'parent' ] != 0) {  
  167.         $this ->categories_string .= CATEGORIES_SUBCATEGORIES_INDENT;  
  168.       }  
  169.     }  
  170.   
  171.     if  ( $this ->tree[ $counter ][ 'parent' ] == 0) {  
  172.       $cPath_new  =  'cPath='  .  $counter ;  
  173.       $this ->box_categories_array[ $ii ][ 'top' ] =  'true' ;  
  174.     } else  {  
  175.       $this ->box_categories_array[ $ii ][ 'top' ] =  'false' ;  
  176.       $cPath_new  =  'cPath='  .  $this ->tree[ $counter ][ 'path' ];  
  177.       $this ->categories_string .= CATEGORIES_SEPARATOR_SUBS;  
  178.     }  
  179.     $this ->box_categories_array[ $ii ][ 'path' ] =  $cPath_new ;  
  180.     if  (isset( $cPath_array ) && in_array( $counter$cPath_array )) {  
  181.       $this ->box_categories_array[ $ii ][ 'current' ] = true;  
  182.     } else  {  
  183.       $this ->box_categories_array[ $ii ][ 'current' ] = false;  
  184.     }  
  185.     // display category name   
  186.     $this ->box_categories_array[ $ii ][ 'name' ] =  $this ->categories_string .  $this ->tree[ $counter ][ 'name' ];  
  187.     // make category image available in case needed   
  188.     $this ->box_categories_array[ $ii ][ 'image' ] =  $this ->tree[ $counter ][ 'image' ];  
  189.     if  (zen_has_category_subcategories( $counter )) {  
  190.       $this ->box_categories_array[ $ii ][ 'has_sub_cat' ] = true;  
  191.     } else  {  
  192.       $this ->box_categories_array[ $ii ][ 'has_sub_cat' ] = false;  
  193.     }  
  194.     if  (SHOW_COUNTS ==  'true' ) {  
  195.       $products_in_category  = zen_count_products_in_category( $counter );  
  196.       if  ( $products_in_category  > 0) {  
  197.         $this ->box_categories_array[ $ii ][ 'count' ] =  $products_in_category ;  
  198.       } else  {  
  199.         $this ->box_categories_array[ $ii ][ 'count' ] = 0;  
  200.       }  
  201.     }  
  202.     if  ( $this ->tree[ $counter ][ 'next_id' ] != false) {  
  203.       $ii ++;  
  204.       $this ->zen_show_category( $this ->tree[ $counter ][ 'next_id' ],  $ii );  
  205.     }  
  206.     return   $this ->box_categories_array;  
  207.   }  
  208. }  
  209. ?> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值