PHP uses recursion to generate article trees_PHP tutorial

WBOY
Release: 2016-07-13 09:56:34
Original
1325 people have browsed it

PHP uses recursion to generate article trees

When writing recursive functions, you can consider caching and defining some static variables to store the results of the previous run. Multi-program operation efficiency is very helpful. Probably The steps are as follows: first get the data from the database, put it into an array, then convert the data into a tree-shaped array, and finally convert the tree-shaped array into html code. Let’s take a look at an example

Because I have a technical website that mainly focuses on articles, and some of the articles are in a series, so I want to classify these articles and put them under the same category.

The database is well designed. It is nothing more than using id and fatherid to classify. fatherid represents the parent category and is the id of the article. id is the unique id of the article. There is no limit to the level. It can be two or three levels. A fatherid of 0 indicates a top-level article.

PHP code, mainly recursive

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

function category_tree($fatherid){

//require_once("mysql_class/config.inc.php");

//require_once("mysql_class/Database.class.php");

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);

$db->connect();

$sql = "SELECT id,title,url FROM ".TABLE_TASK."

WHERE fatherid=$fatherid and ispublic=1 order by id asc";

$articles = $db->query($sql);

$db->close();

while ($record = $db->fetch_array($articles)){

$i = 0;

if ($i == 0){

if($fatherid==0){

echo '

    ';

    }else{

    echo '

    ';

    }

    }

    }

1

2

3

1

category_tree(0) //先提取最顶层文章

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
function category_tree($fatherid){ //require_once("mysql_class/config.inc.php"); //require_once("mysql_class/Database.class.php"); $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); $db->connect(); $sql = "SELECT id,title,url FROM ".TABLE_TASK." WHERE fatherid=$fatherid and ispublic=1 order by id asc"; $articles = $db->query($sql); $db->close(); while ($record = $db->fetch_array($articles)){ $i = 0; if ($i == 0){ if($fatherid==0){ echo '
    '; }else{ echo ''; } } }
Call:  ?
1 category_tree(0) //Extract the top-level article first

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987717.htmlTechArticlePHP uses recursion to generate article trees to write recursive functions. You can consider caching and define some static variables to store the last run. As a result, multi-program running efficiency is very helpful. The approximate steps are as follows: First go to...
Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template