Example php+mysql query to achieve unlimited lower-level classification tree output

coldplay.xixi
Release: 2023-04-09 14:02:02
forward
3400 people have browsed it

Example php+mysql query to achieve unlimited lower-level classification tree output

The example in this article describes the implementation of unlimited lower-level classification tree output through php mysql query. Share it with everyone for your reference, the details are as follows:

The PHP introduced here combined with mysql to query the infinite subordinate tree output is actually infinite classification. I have compiled several examples of unlimited PHP classification for you.

[Related learning recommendations:php programming(video),mysql video tutorial]

Tree output:

function get_array($user_id,$top=0){ global $mysql,$_G; $sql = "select user_id as name from `{spreads_users}` where p1.spreads_userid='{$user_id}'"; $rows= $mysql->db_fetch_arrays($sql); if($top==1){ $arr[0]['name']=$user_id; $arr[0]['children']=array(); } $top=$top+1; foreach ($rows as $key=>$value) { $r = get_array($value['name']); //调用函数,传入参数,继续查询下级 $arr[0]['children'][$key]['name']= $value['username']; //组合数组 if(is_array($r)){ $arr[0]['children'][$key]['children']= $r[0]['children']; } $i++; } return $arr; } $list = get_array("1000",1); //调用函数1000是顶级ID echo 'var data='.json_encode($list);
Copy after login

This is the output Array and then transferred to json

Example:

Table structure: the id field is the classification identifier, and the name field is the classification name , the father_id field is the id of the parent category to which it belongs, the path field is the category path, which stores the collection of ancestors of the category, isdir determines whether it is a directory, 1 means yes, 0 means no.

Display function:

//$count为分类等级 sort_list($str,$fatherid,$count) { $rs = $this->sql->re_datas("select * from sort where father_id = fatherid"); $num = $this->sql->sql_numrows(); $i=0; $n = 1; while(isset($rs[$i])) { $name = ""; for($n = 1 ; $n < $count ; $n ) { $name.="│ "; } if($i 1==$num) { $name.="└─".$rs[$i][name]; } else { $name.="├─".$rs[$i][name]; } if($rs[$i][isdir]) { $str.="".$name.""; } else { $str.=$name"; } $temp = $count 1; $str = $this->sort_list($str,$rs[$i][id],$temp); $i ; } return $str; }
Copy after login

The $this->sql object is the sql operation class object, the re_datas() function returns the found array, and the sql_numrows() function returns the queried number.

Call Method:

$sort_list = sort_list($sort_list,0,1);
Copy after login

Example:

Table:category

id int Primary key, auto-increment
name varchar Category name
pid int Parent class id , the default is 0

The pid of the top category is 0 by default. When we want to take out the sub-category tree of a certain category, the basic idea is recursion. Of course, due to efficiency issues, it is not recommended every time The database is queried recursively. The usual approach is to first extract all categories, save them in a PHP array, and then process them. Finally, the results can be cached to improve the efficiency of the next request.
Let’s first build an original array , this can be pulled directly from the database:

$categories = array( array('id'=>1,'name'=>'电脑','pid'=>0), array('id'=>2,'name'=>'手机','pid'=>0), array('id'=>3,'name'=>'笔记本','pid'=>1), array('id'=>4,'name'=>'台式机','pid'=>1), array('id'=>5,'name'=>'智能机','pid'=>2), array('id'=>6,'name'=>'功能机','pid'=>2), array('id'=>7,'name'=>'超级本','pid'=>3), array('id'=>8,'name'=>'游戏本','pid'=>3), );
Copy after login

The goal is to convert it into the following structure:

电脑 —笔记本 ——-超级本 ——-游戏本 —台式机 手机 —智能机 —功能机
Copy after login

If it is represented by an array, a children key can be added to store it Subcategory:

array( //1对应id,方便直接读取 1 => array( 'id'=>1, 'name'=>'电脑', 'pid'=>0, children=>array( &array( 'id'=>3, 'name'=>'笔记本', 'pid'=>1, 'children'=>array( //此处省略 ) ), &array( 'id'=>4, 'name'=>'台式机', 'pid'=>1, 'children'=>array( //此处省略 ) ), ) ), //其他分类省略 )
Copy after login

Processing process:

$tree = array(); //第一步,将分类id作为数组key,并创建children单元 foreach($categories as $category){ $tree[$category['id']] = $category; $tree[$category['id']]['children'] = array(); } //第二部,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。 foreach ($tree as $k=>$item) { if ($item['pid'] != 0) { $tree[$item['pid']]['children'][] = &$tree[$k]; } }
Copy after login

print_r($tree);The print result is as follows:

Array ( [1] => Array ( [id] => 1 [name] => 电脑 [pid] => 0 [children] => Array ( [0] => Array ( [id] => 3 [name] => 笔记本 [pid] => 1 [children] => Array ( [0] => Array ( [id] => 7 [name] => 超级本 [pid] => 3 [children] => Array ( ) ) [1] => Array ( [id] => 8 [name] => 游戏本 [pid] => 3 [children] => Array ( ) ) ) ) [1] => Array ( [id] => 4 [name] => 台式机 [pid] => 1 [children] => Array ( ) ) ) ) [2] => Array ( [id] => 2 [name] => 手机 [pid] => 0 [children] => Array ( [0] => Array ( [id] => 5 [name] => 智能机 [pid] => 2 [children] => Array ( ) ) [1] => Array ( [id] => 6 [name] => 功能机 [pid] => 2 [children] => Array ( ) ) ) ) [3] => Array ( [id] => 3 [name] => 笔记本 [pid] => 1 [children] => Array ( [0] => Array ( [id] => 7 [name] => 超级本 [pid] => 3 [children] => Array ( ) ) [1] => Array ( [id] => 8 [name] => 游戏本 [pid] => 3 [children] => Array ( ) ) ) ) [4] => Array ( [id] => 4 [name] => 台式机 [pid] => 1 [children] => Array ( ) ) [5] => Array ( [id] => 5 [name] => 智能机 [pid] => 2 [children] => Array ( ) ) [6] => Array ( [id] => 6 [name] => 功能机 [pid] => 2 [children] => Array ( ) ) [7] => Array ( [id] => 7 [name] => 超级本 [pid] => 3 [children] => Array ( ) ) [8] => Array ( [id] => 8 [name] => 游戏本 [pid] => 3 [children] => Array ( ) ) )
Copy after login

Advantages: The relationship is clear, and it is simple to modify the superior-subordinate relationship.

Disadvantages: Using PHP to process, if the number of categories is huge, the efficiency will also be reduced.

Related recommendations:Programming video course

The above is the detailed content of Example php+mysql query to achieve unlimited lower-level classification tree output. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!