Leftnav.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace org;
  3. class leftnav{
  4. static public function rule($cate , $lefthtml = '— — ' , $pid=0 , $lvl=0, $leftpin=0 ){
  5. $arr=array();
  6. for($i=0;$i<count($cate);$i++){
  7. $sort[] = $cate[$i]['sort'];
  8. }
  9. array_multisort($sort, SORT_ASC, $cate);
  10. foreach ($cate as $v){
  11. if($v['pid']==$pid){
  12. $v['lvl']=$lvl + 1;
  13. $v['leftpin']=$leftpin + 0;//左边距
  14. // $v['lefthtml']=str_repeat($lefthtml,$lvl);
  15. $v['title'] = str_repeat($lefthtml,$lvl).$v['title'];
  16. $arr[]=$v;
  17. $arr= array_merge($arr,self::rule($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
  18. }
  19. }
  20. return $arr;
  21. }
  22. /**
  23. * 得到树型结构
  24. * @param int ID,表示获得这个ID下的所有子级
  25. * @param string 生成树型结构的基本代码,例如:"<option value=\$id \$selected>\$spacer\$name</option>"
  26. * @param int 被选中的ID,比如在做树型下拉框的时候需要用到
  27. * @return string
  28. */
  29. public function get_tree($myid, $str="", $sid = 0, $adds = '', $str_group = '')
  30. {
  31. $number = 1;
  32. //一级栏目
  33. $child = $this->get_child($myid);
  34. if (is_array($child)) {
  35. $total = count($child);
  36. foreach ($child as $id => $value) {
  37. $j = $k = '';
  38. if ($number == $total) {
  39. $j .= $this->icon[2];
  40. } else {
  41. $j .= $this->icon[1];
  42. $k = $adds ? $this->icon[0] : '';
  43. }
  44. $spacer = $adds ? $adds . $j : '';
  45. $selected = $id == $sid ? 'selected' : '';
  46. extract($value);
  47. $pid == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";");
  48. // $value['title'] = $spacer.$value['title'];
  49. $value['placeholder'] = $spacer;
  50. $this->end[] = $value;
  51. // $this->ret .= $nstr;
  52. $nbsp = $this->nbsp;
  53. $this->get_tree($id, $str, $sid, $adds . $k . $nbsp, $str_group);
  54. $number++;
  55. }
  56. }
  57. return $this->end;
  58. }
  59. public $end = array();
  60. /**
  61. * 得到子级数组
  62. * @param int
  63. * @return array
  64. */
  65. public function get_child($myid)
  66. {
  67. $a = $newarr = array();
  68. if (is_array($this->arr)) {
  69. foreach ($this->arr as $id => $a) {
  70. if ($a['pid'] == $myid) {
  71. $newarr[$id] = $a;
  72. }
  73. }
  74. }
  75. return $newarr ? $newarr : false;
  76. }
  77. /**
  78. * 生成树型结构所需要的2维数组
  79. * @var array
  80. */
  81. public $arr = array();
  82. /**
  83. * 生成树型结构所需修饰符号,可以换成图片
  84. * @var array
  85. */
  86. public $icon = array('&nbsp;│&nbsp;', '&nbsp;├─&nbsp;', '&nbsp;└─&nbsp;');
  87. public $nbsp = "&nbsp;";
  88. /**
  89. * @access private
  90. */
  91. public $ret = '';
  92. public function init($arr = array())
  93. {
  94. for($i=0;$i<count($arr);$i++){
  95. $sort[] = $arr[$i]['sort'];
  96. }
  97. array_multisort($sort, SORT_ASC, $arr);
  98. $this->arr = $arr;
  99. $this->ret = '';
  100. return is_array($arr);
  101. }
  102. }