• 博客访问: 4969
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
  • 认证徽章:
文章分类
文章存档

2016年(48)

我的朋友
微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

订阅
热词专题

分类: PHP

wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改。本文分享其中关于基于PHP的通用的树型类代码,供技术员参考学习。

点击(此处)折叠或打开

  1. <?php
  2. /**
  3.  +------------------------------------------------
  4.  * 通用的树型类
  5.  * 请使用的朋友注意 id, name, pid 这三个下标, id的值与数组下标必须吻合
  6.  +------------------------------------------------
  7.  * @author yangyunzhou@foxmail.com
  8.  +------------------------------------------------
  9.  * @date 2010年11月23日10:09:31
  10.  +------------------------------------------------
  11.  */
  12. class Tree {
  13.     
  14.     /**
  15.      * +------------------------------------------------
  16.      * 生成树型结构所需要的2维数组
  17.      * +------------------------------------------------
  18.      *
  19.      * @author yangyunzhou@foxmail.com
  20.      * +------------------------------------------------
  21.      * @var Array
  22.      */
  23.     var $arr = array ();
  24.     
  25.     /**
  26.      * +------------------------------------------------
  27.      * 生成树型结构所需修饰符号,可以换成图片
  28.      * +------------------------------------------------
  29.      *
  30.      * @author yangyunzhou@foxmail.com
  31.      * +------------------------------------------------
  32.      * @var Array
  33.      */
  34.     var $icon = array (
  35.             '│',
  36.             '├─',
  37.             '└─'
  38.     );
  39.     
  40.     /**
  41.      *
  42.      * @access private
  43.      */
  44.     var $ret = '';
  45.     
  46.     /**
  47.      * 构造函数,初始化类
  48.      *
  49.      * @param
  50.      *     array 2维数组,例如:
  51.      *     array(
  52.      *     1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
  53.      *     2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'),
  54.      *     3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'),
  55.      *     4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'),
  56.      *     5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
  57.      *     6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
  58.      *     7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
  59.      *     )
  60.      */
  61.     function tree($arr = array()) {
  62.         $this->arr = $arr;
  63.         $this->ret = '';
  64.         return is_array ( $arr );
  65.     }
  66.     
  67.     /**
  68.      * 得到父级数组
  69.      *
  70.      * @param
  71.      *     int
  72.      * @return array
  73.      */
  74.     function get_parent($myid) {
  75.         $newarr = array ();
  76.         if (! isset ( $this->arr [$myid] ))
  77.             return false;
  78.         $pid = $this->arr [$myid] ['pid'];
  79.         $pid = $this->arr [$pid] ['pid'];
  80.         if (is_array ( $this->arr )) {
  81.             foreach ( $this->arr as $id => $a ) {
  82.                 if ($a ['pid'] == $pid)
  83.                     $newarr [$id] = $a;
  84.             }
  85.         }
  86.         return $newarr;
  87.     }
  88.     
  89.     /**
  90.      * 得到子级数组
  91.      *
  92.      * @param
  93.      *     int
  94.      * @return array
  95.      */
  96.     function get_child($myid) {
  97.         $a = $newarr = array ();
  98.         if (is_array ( $this->arr )) {
  99.             foreach ( $this->arr as $id => $a ) {
  100.                 if ($a ['pid'] == $myid)
  101.                     $newarr [$id] = $a;
  102.             }
  103.         }
  104.         return $newarr ? $newarr : false;
  105.     }
  106.     
  107.     /**
  108.      * 得到当前位置数组
  109.      *
  110.      * @param
  111.      *     int
  112.      * @return array
  113.      */
  114.     function get_pos($myid, &$newarr) {
  115.         $a = array ();
  116.         if (! isset ( $this->arr [$myid] ))
  117.             return false;
  118.         $newarr [] = $this->arr [$myid];
  119.         $pid = $this->arr [$myid] ['pid'];
  120.         if (isset ( $this->arr [$pid] )) {
  121.             $this->get_pos ( $pid, $newarr );
  122.         }
  123.         if (is_array ( $newarr )) {
  124.             krsort ( $newarr );
  125.             foreach ( $newarr as $v ) {
  126.                 $a [$v ['id']] = $v;
  127.             }
  128.         }
  129.         return $a;
  130.     }
  131.     
  132.     /**
  133.      * -------------------------------------
  134.      * 得到树型结构
  135.      * -------------------------------------
  136.      *
  137.      * @author yangyunzhou@foxmail.com
  138.      * @param $myid 表示获得这个ID下的所有子级     
  139.      * @param $str 生成树形结构基本代码,
  140.      *     例如: "<option value=\$id \$select>\$spacer\$name</option>"
  141.      * @param $sid 被选中的ID,
  142.      *     比如在做树形下拉框的时候需要用到
  143.      * @param
  144.      *     $adds
  145.      * @param
  146.      *     $str_group
  147.      */
  148.     function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '') {
  149.         $number = 1;
  150.         $child = $this->get_child ( $myid );
  151.         if (is_array ( $child )) {
  152.             $total = count ( $child );
  153.             foreach ( $child as $id => $a ) {
  154.                 $j = $k = '';
  155.                 if ($number == $total) {
  156.                     $j .= $this->icon [2];
  157.                 } else {
  158.                     $j .= $this->icon [1];
  159.                     $k = $adds ? $this->icon [0] : '';
  160.                 }
  161.                 $spacer = $adds ? $adds . $j : '';
  162.                 $selected = $id == $sid ? 'selected' : '';
  163.                 @extract ( $a );
  164.                 $parentid == 0 && $str_group ? eval ( "\$nstr = \"$str_group\";" ) : eval ( "\$nstr = \"$str\";" );
  165.                 $this->ret .= $nstr;
  166.                 $this->get_tree ( $id, $str, $sid, $adds . $k . '&nbsp;', $str_group );
  167.                 $number ++;
  168.             }
  169.         }
  170.         return $this->ret;
  171.     }
  172.     
  173.     /**
  174.      * 同上一方法类似,但允许多选
  175.      */
  176.     function get_tree_multi($myid, $str, $sid = 0, $adds = '') {
  177.         $number = 1;
  178.         $child = $this->get_child ( $myid );
  179.         if (is_array ( $child )) {
  180.             $total = count ( $child );
  181.             foreach ( $child as $id => $a ) {
  182.                 $j = $k = '';
  183.                 if ($number == $total) {
  184.                     $j .= $this->icon [2];
  185.                 } else {
  186.                     $j .= $this->icon [1];
  187.                     $k = $adds ? $this->icon [0] : '';
  188.                 }
  189.                 $spacer = $adds ? $adds . $j : '';
  190.                 
  191.                 $selected = $this->have ( $sid, $id ) ? 'selected' : '';
  192.                 @extract ( $a );
  193.                 eval ( "\$nstr = \"$str\";" );
  194.                 $this->ret .= $nstr;
  195.                 $this->get_tree_multi ( $id, $str, $sid, $adds . $k . '&nbsp;' );
  196.                 $number ++;
  197.             }
  198.         }
  199.         return $this->ret;
  200.     }
  201.     function have($list, $item) {
  202.         return (strpos ( ',,' . $list . ',', ',' . $item . ',' ));
  203.     }
  204.     
  205.     /**
  206.      * +------------------------------------------------
  207.      * 格式化数组
  208.      * +------------------------------------------------
  209.      *
  210.      * @author yangyunzhou@foxmail.com
  211.      * +------------------------------------------------
  212.      */
  213.     function getArray($myid = 0, $sid = 0, $adds = '') {
  214.         $number = 1;
  215.         $child = $this->get_child ( $myid );
  216.         if (is_array ( $child )) {
  217.             $total = count ( $child );
  218.             foreach ( $child as $id => $a ) {
  219.                 $j = $k = '';
  220.                 if ($number == $total) {
  221.                     $j .= $this->icon [1];
  222.                 } else {
  223.                     $j .= $this->icon [1];
  224.                     $k = $adds ? $this->icon [0] : '';
  225.                 }
  226.                 $spacer = $adds ? $adds . $j : '';
  227.                 @extract ( $a );
  228.                 $a ['name'] = $spacer . ' ' . $a ['name'];
  229.                 $this->ret [$a ['id']] = $a;
  230.                 $fd = $adds . $k . '&nbsp;&nbsp;&nbsp;&nbsp;';
  231.                 $this->getArray ( $id, $sid, $fd );
  232.                 $number ++;
  233.             }
  234.         }
  235.         
  236.         return $this->ret;
  237.     }
  238. }
  239. ?>

 原文详情地址:http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

WeMall - 开源微商城 微信商城 商城源码 分销商城 b2b2c商城系统

 

 

阅读(785) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册