-
-
< - /**
- * Function: Query all categories to generate Tree menu
- * @param int $pid parent ID, query starts from the top level by default
- */
- function toTree($pid=0){
- //Query all top categories
- //Not much to say about the database connection here
- $model=M('Category'); Instantiate the model
- //Query map conditions
- $map=array(
- 'pid'=>$pid,
- );
- //Query
- $data=$model- >where($map)->select();
- //Define a new array to store the generated html tree menu
- $html_array=array();
- //Loop classification
- $html='';
- $padding=0; //Indentation
- foreach($data as $k=>$v){
- //Indentation is implemented based on the value of the path field
- $path=$v['path'];
- if( $path&&strpos($v['path'],'-')){
- $path_array=@explode('-',$v['path']);
- $count=count($path_array)-1;
- $padding=$count*20; //The next level classification is indented by 20 pixels
- }
- //Current classification data html
- $data_array[]='
- '.$v['name'].'
- ';< ;/p>
//Recursively start to search for lower-level categories
- $data_array[]=toTree($v['id']);//Recurse using the ID of the current category as the parent ID
- }
- / /Foreach loop ends and merges classified html arrays
- $html.=implode('',$data_array());
- $html.='';
- //HTML tree menu construction is completed, it’s that simple, so easy!
- return $html;
- }
-
Copy code
|