首页 > 编程技术 > php

PHP实现多级分类生成树的方法示例

发布时间:2017-7-6 23:49

小编推荐的这篇文章介绍了PHP实现多级分类生成树的方法示例,非常实用,有兴趣的同学快来看看吧。

条件,数据库里分类是按id,fid(父ID)实现多级分类的!

使用方法:

 代码如下复制代码

$sql="XXXXXXXXXX";  //sql语句

$res=$db->Select($sql); //执行sql

$list=array();

treeList(treeGet($res),$list);  /生成树

print_r($res); //打印出来看看!

使用结果:

┣推荐新闻啊

┃┣国际新闻

┃┣dfffffg

┃┣ttttttt

┃┃┗yyyyy

代码如下:

 代码如下复制代码

/**

* 选择SQL涵数

*

* @access public

* @param Array  $field  字段信息,支持涵数

* @param Array  $table  数据库表

* @param Array  $where  条件

* @return SQL SQL语句

*/

functiontreeGet($data)

{

  $tmptree=null;

  $tree=$data;

  returntreeAddNodeToTree($tmptree,treegetbyuid($tree,0,@$field),$tree);

}

/**

*插入SQL涵数

*

* @access public

* @param Array  $fieldResult  字段信息,支持涵数

* @param Array  $table  数据库表

* @return SQL SQL语句

*/

functiontreeAddNodeToTree($Node,$miniTree,&$source)

{

  if(is_array($miniTree)) {

    foreach($miniTreeas$k=>$v)

    {

      if(!count($miniTree[$k]['child']=treeAddNodeToTree($miniTree[$k],treegetbyuid($source,@$v['id']),$source)))

      {

        unset($miniTree[$k]['child']);

        $miniTree[$k]['leaf']=true;//设置叶结点

      }

    }

    return$Node['child']=$miniTree;

  }

}

functiontreegetbyuid(&$stree,$uid)

{

  $dtree=array();

  if(is_array($stree)){

    foreach($streeas$k=>$v)

    {

      if($v['fid']==$uid)

      {

        $mytmp=array();

        $mytmp=$v;

        unset($stree[$k]);

        array_push($dtree,$mytmp);

        $mytmp=null;

      }

    }

  }

  return$dtree;

}

/**

*更新SQL涵数

*

* @access public

* @param Array  $fieldResult  字段信息,支持涵数

* @param Array  $table  数据库表

* @param Array  $where  条件

* @return SQL SQL语句

*/

functiontreeMakeDeep($deep)

{

  $returnValue="";

  for(;$deep;$deep--)

  {

    $returnValue.="┃";

  }

  return$returnValue."┣";

}

functiontreeList($treeData,&$List)

{

  static$deep=0;

  if(is_array($treeData))

  {

    foreach($treeDataas$k=>$v)

    {

      $v['deepValue']=treeMakeDeep($deep);

      $v['deep']=$deep;

      $t=$v;

      unset($t['child']);

      array_push($List,$t);

      if($v['child'])

      {

        ++$deep;

        $optionsNode.=treeList($v['child'],$List);

        $deep--;

      }

    }

    if($lastV=array_pop($List))

    {

      $lastV['deepValue']=str_replace('┣','┗',$lastV['deepValue']);

      array_push($List,$lastV);

    }

  }

}

functiontreeSelect($tree,$id,$options="child")

{

  switch(strtolower($options))

  {

    case"child":

    $tmpTree=array();

    $deep=-1;

    foreach($treeas$k=>$v)

    {

      if($id==$v['id'])

      {

        array_push($tmpTree,$v);

        $deep=$v['deep'];

      }elseif($deep!=-1)

      {

        if($v['deep']<=$deep)

        {

          break;

        }else

        {

          array_push($tmpTree,$v);

        }

      }

    }

    break;

    case"remove":

    default:

    $tmpTree=array();

    $deep=-1;

    foreach($treeas$k=>$v)

    {

      if($id==$v['id'])

      {

        $deep=$v['deep'];

        continue;

      }elseif($deep!=-1)

      {

        if($v['deep']<=$deep)

        {

          array_push($tmpTree,$v);

          $deep=-1;

        }

        continue;

      }

      array_push($tmpTree,$v);

    }

  }

  return$tmpTree;

}

小编推荐的这篇文章介绍了PHP常用排序算法实例小结,基本排序,冒泡排序,快速排序,插入排序,非常实用,有兴趣的同学快来看看吧。
 代码如下复制代码

classbevin{

public$public='public'

private$private='private'

protected$protected='protected'

//final $final = 'final'

static$static='static'

function__construct(){

 $this->protected='change private'

}

publicfunctionsetValue($a){

 self::$static=$a;

}

publicfunctiongetValue(){

 echo$this->private;

}

function__destruct(){

 echo'asdfsadf'

}

}

classpaixu {

// 基本排序

publicfunctiont_sortArray($array) {

 if(is_array($array) &&count($array)>1) {

 for($i=0;$i<count($array);$i++) {

  for($j=($i+1);$j<count($array);$j++) {

  $temp=$array[$i];

  if($array[$j]<$array[$i]) {

  $array[$i] =$array[$j];

  $array[$j] =$temp;

  }

  }

 }

 return$array;

 }else{

 return$array;

 }

}

// 冒泡排序

publicfunctionc_sortArray($array) {

 if(!is_array($array) ||count($array)<=1){return$array; }

 $status= false;

 foreach($arrayas$key=>$v) {

 if($key>0) {

  if($array[$key-1]>$array[$key]) {

  $array[$key] =$array[$key-1];

  $array[$key-1] =$v;

  $status= true;

  }

 }

 }

 if($status) {

 return$this->c_sortArray($array);

 }else{

 return$array;

 }

}

// 快速排序

publicfunctionv_sortArray($array) {

 if(!is_array($array) ||count($array)<=1){return$array; }

 if(count($array)>2) {

 $m=$array[floor(count($array)/2)+1];

 }else{

 if($array[0]>$array[1]) {

  $temp=$array[0];

  $array[0] =$array[1];

  $array[1] =$temp;

 }

 return$array;

 }

 $leftarray=array();

 $rightarray=array();

 foreach($arrayas$key=>$v) {

 if($v>$m) {

  $rightarray[] =$v;

 }

 if($v<$m) {

  $leftarray[] =$v;

 }

 if($v==$m) {

  $mid[] =$v;

 }

 }

 $nleftarray=$this->v_sortArray($leftarray);

 $nrightarray=$this->v_sortArray($rightarray);

 returnarray_merge($nleftarray,$mid,$nrightarray);

}

// 直接插入排序

publicfunctioni_sortArray($array) {

 if(!is_array($array) ||count($array)<=1){return$array; }

 $newarray=array($array[0]);

 $temp= 0;

 foreach($arrayas$k=>$v) {

 if($k>0) {

  if($v>=$newarray[count($newarray)-1]) {

  $newarray[] =$v;

  }else{

  foreach($newarrayas$nk=>$nv) {

  if($v<$nv) {

  $temparray=array();

  foreach($newarrayas$ck=>$cv) {

   if($ck<$nk) {

   $temparray[$ck] =$cv;

   }elseif($ck==$nk) {

   $temparray[$ck] =$v;

   $temparray[($ck+1)] =$cv;

   }else{

   $temparray[($ck+1)] =$cv;

   }

  }

  $newarray=$temparray;

  break;

  }

  }

  }

 }

 }

 return$newarray;

}

}

$bevin=newpaixu;

$array=array(5,4,5,4,4,5,5,5,5,5);

$v=$bevin->t_sortArray($array);

print_r($v);

$v=$bevin->c_sortArray($array);

print_r($v);

$v=$bevin->v_sortArray($array);

print_r($v);

$v=$bevin->i_sortArray($array);

print_r($v);

运行结果:

 代码如下复制代码

Array

(

  [0] => 4

  [1] => 4

  [2] => 4

  [3] => 5

  [4] => 5

  [5] => 5

  [6] => 5

  [7] => 5

  [8] => 5

  [9] => 5

)

Array

(

  [0] => 4

  [1] => 4

  [2] => 4

  [3] => 5

  [4] => 5

  [5] => 5

  [6] => 5

  [7] => 5

  [8] => 5

  [9] => 5

)

Array

(

  [0] => 4

  [1] => 4

  [2] => 4

  [3] => 5

  [4] => 5

  [5] => 5

  [6] => 5

  [7] => 5

  [8] => 5

  [9] => 5

)

Array

(

  [0] => 4

  [1] => 4

  [2] => 4

  [3] => 5

  [4] => 5

  [5] => 5

  [6] => 5

  [7] => 5

  [8] => 5

  [9] => 5

)

小编推荐的这篇文章介绍了php实现图片按比例截取的方法,非常实用,有兴趣的同学快来看看吧。

 

 代码如下复制代码

filename ='img/test.jpg'

$all_type=array(

"jpg" =>array("create"=>"ImageCreateFromjpeg","output"=>"imagejpeg" ,"exn"=>".jpg"),

"gif" =>array("create"=>"ImageCreateFromGIF","output"=>"imagegif" ,"exn"=>".gif"),

"jpeg" =>array("create"=>"ImageCreateFromjpeg","output"=>"imagejpeg" ,"exn"=>".jpg"),

"png" =>array("create"=>"imagecreatefrompng","output"=>"imagepng" ,"exn"=>".png"),

"wbmp" =>array("create"=>"imagecreatefromwbmp","output"=>"image2wbmp","exn"=>".wbmp")

);

$imgtype=getimagesize($filename);

$width=$imgtype[0];

$height=$imgtype[1];

$type=str_replace('image/','',$imgtype['mime']);

$func_create=$all_type[$type]['create'];

$func_output=$all_type[$type]['output'];

$x=$y=0;

if(($width* 100)>($height* 120))

{

$newwidth=ceil($height* 120/100);

$newheight=$height;

$x= ($width-$newwidth)/2;

}

elseif(($width* 100)<($height* 120))

{

$newheight=ceil($width* 100/120);

$newwidth=$width;

$y= ($height-$newheight)/2;

}

else

{

$newheight=$height;

$newwidth=$width;

}

// Load

$thumb= imagecreatetruecolor($newwidth,$newheight);

$source=$func_create($filename);

// Resize

imagecopyresized($thumb,$source, 0, 0, 0, 0,$newwidth,$newheight,$newwidth,$newheight);

// Output

$func_output($thumb,'a.jpeg');

 

PHPCMS手机站伪静态怎么设置?本文详细介绍了PHPCMS手机站伪静态设置详细教程非常实用,有兴趣的同学快来看看吧!

1、打开/phpcms/modules/wap/functions/global.func.php

找到里面的这两个函数,如下图所示的注释掉的那行代码,在下面加入一行代码。

 代码如下复制代码

functionlist_url($typeid) {

  #returnWAP_SITEURL."&a=lists&typeid=$typeid";

  return"/list-$typeid".'.html'

}

functionshow_url($catid,$id,$typeid='') {

global$WAP;

if($typeid=='') {

 $types= getcache('wap_type','wap');

 foreach($typesas$type) {

  if($type['cat']==$catid) {

  $typeid=$type['typeid'];

  break;

  }

 }

  #returnWAP_SITEURL."&a=show&catid=$catid&typeid=$typeid&id=$id";

  return"/show-$catid-$typeid-$id-1".'.html'

}

 

2、打开/phpcms/modules/wap/index.php

找到第59行,如下图所示的注释掉的那行代码,在下面加入一行代码。

 代码如下复制代码

#define('URLRULE','index.php?m=wap&c=index&a=lists&typeid={$typeid}~index.php?m=wap&c=index&a=lists&typeid={$typeid}&page={$page}');

define('URLRULE','list-{$typeid}.html~list-{$typeid}-{$page}.html');

3、添加伪静态规则

我使用的是apache服务器,把apache服务器设置支持.htaccess文件,在网站根目录创建.htaccess文件,在里面添加代码:

 代码如下复制代码

RewriteEngine On 

RewriteRule ^list-([0-9]+)-([0-9]+) index.php?&a=lists&typeid=$1&page=$2

RewriteRule ^list-([0-9]+) index.php?&a=lists&typeid=$1;

RewriteRule ^show-([0-9]+)-([0-9]+)-([0-9]+) index.php?a=show&catid=$1&typeid=$2&id=$3;

这样,phpcms手机站就变成伪静态了。

.htaccess如何设置和创建可自行百度一下,方法都比较简单,这里就不细说了。

如果是nginx服务器,重写规则这样写:

 代码如下复制代码

rewrite ^/list-([0-9]+).html /index.php?&a=lists&typeid=$1last;

rewrite ^/show-([0-9]+)-([0-9]+)-([0-9]+)-1.html /index.php?a=show&catid=$1&typeid=$2&id=$3last;

标签:[!--infotagslink--]

您可能感兴趣的文章: