PHP Infinite Classification Implementation Program _php tutorial

Source: Internet
Author: User
1, the database by setting the parent class ID to make a unique index, and then use the recursive invocation of the function to implement an infinite classification, 2, the database design is arranged in a specific format, and then use the MySQL query key function: Concat. Program implementation is relatively simple

First we assume that there is a class three classification, news →php News →php6.0 out.
If we are looking for "PHP6.0 out" This news, we first click on the news, then click on the PHP News

Can be found out, that is to say we can go through the grandfather class level down, in turn we just

If you know the parent class of a subclass, you can find it. So we can design the database with more

A field with a parent ID can be used to implement an infinitely categorized function.

copy code
//we build a table "class"
CREATE table ' class ' (
' ID ' int (one) not NULL auto_increment COMMENT ' class ID ',
' f_id ' int (one) not NULL COMMENT ' parent ID ',
' name ' varchar (+) Coll Ate gbk_bin not NULL COMMENT ' class name ',
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=gbk collate=gbk_bin auto_in Crement=1;


First we insert the big category ' News ' into the database, because ' news ' is the largest category, there is no parent, so I set its f_id to 0.
INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (1, 0, ' News '); ID This field is automatically growing and can not write values.

Then we insert the ' PHP News ' category into the database and its parent ' news ' ID is 1, so its f_id is set to 1.
INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (2, 1, ' PHP News ');

Then we insert the ' PHP6.0 out ' category into the database, and its parent class ' PHP News ' ID is 2, so its f_id is set to 2.
INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (3, 2, ' PHP6.0 out ');

In the same way, we can continue to insert the classification, we have reached an infinite classification.
We can find that the key to inserting a taxonomy is to find the ID of the parent class of this taxonomy, and then as the value of the f_id field for this taxonomy.
Suppose you want to insert a category ' technology ' with ' news ' at the same level, that is, it is also the largest category, there is no parent class, then its f_id is also set to 0;
INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (4, 0, ' technology ');

Under ' technology ' there is another category ' PHP technology ', then how do we insert it, first find the ' PHP technology ' parent class ' technology ' ID, and then as the value of its own f_id field.
INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (5, 4, ' PHP technology ');

See here, presumably everyone should understand how to insert each classification into the database. No longer an example.

We already know how to insert individual classifications into the database, so how do we list the categories?

The code is as follows Copy Code

Header ("Content-type:text/html;charset=utf-8");
$db =new mysqli ("localhost", "root", "" "," news_php100 "); Instantiate a database connection. Before using this, make sure you have loaded the Mysqli class library, or connect it with mysql_connect.
if (Mysqli_connect_errno ()) {
echo "link failed:". Mysqli_connect_error ();
Exit (); }
$db->query ("Set names UTF8");
$result = $db->query ("Select name from class where f_id=0"); Find the classification of f_id=0, that is, find each big class.
while ($row = $result->fetch_assoc ()) {
echo $row [' name ']. "
"; In this way, each large class is recycled.
}
Similarly, we can recycle the sub-categories of news.
$result = $db->query ("SELECT * from class where f_id=1"); Find the classification of the f_id=1, that is, find the sub-class of ' news '.
while ($row = $result->fetch_assoc ()) {
echo $row [' name ']. "
"; This is the "news" of the sub-class loop out. Note: Only subclasses, not including grandson classes.
}
Writing here, we will find a question, if this classification is a level 10 classification, do we have to write 10 loops to loop it out each subclass? If it is more class classification, it is obviously unrealistic to write.
So what's the solution? We can write a recursive function, f_id as a parameter, and constantly loop each f_id value, that is to say, each of the f_id value of the sub-class loop out.
First, we store the values of each category in a two-dimensional array, which is useful in the recursive function below.
$result = $db->query ("SELECT * from class");
while ($row = $result->fetch_assoc ()) {
$arr []=array ($row [id], $row [f_id], $row [name]; Each row holds information about the id,f_id,name of a category.
}
function Fenlei ($f _id=0) {//$f _id is initialized to 0, which is the start of the cycle from the largest classification.
Global $arr; Declare $arr as a global variable to be referenced in a function.
for ($i =0; $i
The IF ($arr [$i][1]== $f _id) {//$arr [$i][1] represents the f_id value of the $I+1 classification. Start $f_id=0, that is, the classification of the f_id=0 output.
echo $arr [$i][2]. "
"; $arr [$i][1] represents the value of the name of the $i+1 category.
Fenlei ($arr [$i][0]); $arr [$i][1] represents the value of the ID of the $i+1 category. Recursive, that is, their own ID as the f_id parameter to the recycling of their own sub-class.
}
}
}
?>


Three field ID, ParentID, name
The algorithm is also very simple recursive, used to recursion when it is silly, it should be said extremely silly, because in the recursion by querying the data table to obtain all of the subclass, recently enlightened, think of a person on earth can get the method, the following is the code, a class

The code is as follows Copy Code

Class Tree {

/**
* All classified information from the database
* @var Array
*/
var $arr;
/**
* Format below
* var $arr = array (
1 = = Array (' id ' = ' = ' 1′, ' parentid ' =>0, ' name ' = ' first column one '),
2 = = Array (' id ' = ' = ' 2′, ' parentid ' =>0, ' name ' = ' first column two '),
3 = = Array (' id ' = ' = ' 3′, ' parentid ' =>1, ' name ' = ' Two-level column one '),
);*/

/**
* Output Structure
* @var Array
*/
var $tree = array ();
/**
* Depth of tree recursion
* @var int
*/
var $deep = 1;

/**
* Generate a tree-shaped decorated symbol
* @var Array
*/
var $icon = array (' │ ', ' ├ ', ' └ ');
/**
* Generate a subordinate tree structure of the specified ID
* @par AM int $rootid The ID to get the tree structure
* @param string $add prefix used in recursion
* @param bool $parent _end identifies if the parent class is the last
*/
Functio N Gettree ($rootid = 0, $add = ", $parent _end =true) {
$is _top = 1;
$child _arr = $this->getchild ($rootid);
if (Is_array ($child _arr)) {
$cnt = count ($child _arr),
foreach ($child _arr as $key + = $child) {
$cid = $chi ld[' id '];
$child _child = $this->getchild ($cid);
if ($this->deep >1) {
if ($is _top = = 1 && $this->deep > 1) {
$space = $this->icon[1];
if (! $parent _end)
$add. = $this->icon[0];
Else $add. = ';
}

if ($is _top = = $cnt) {
$space = $this->icon[2];
$parent _end = true;
}else {
$space = $this->icon[1];
$parent _end = false;
}
}
$this->tree[] = array (' spacer ' = = $add. $k. $space,
' Name ' = $child [' name '],
' id ' = $cid
);
$is _top++;

$this->deep++;
if ($this->getchild ($cid))
$this->gettree ($cid, $add, $parent _end);
$this->deep–;

}

}
return $this->tree;
}

/**
* Get sub-category array
* @param int $root
*/
function Getchild ($root = 0) {

$a = $child = Array ();
foreach ($this->arr as $id = + $a) {
if ($a [' parentid '] = = $root) {
$child [$a [' id ']] = $a;
}
}
Return $child? $child: false;

}
/**
* Set Source Array
* @param $arr
*/
function Setarr ($arr = Array ()) {
$this->arr = $arr;
}
}

Through a query to save the structure into an array, and then the recursive operation of the array, no doubt greatly improve the efficiency of the program operation
Using the code is simple


http://www.bkjia.com/PHPjc/444660.html www.bkjia.com true http://www.bkjia.com/PHPjc/444660.html techarticle 1, the database by setting the parent class ID to make a unique index, and then use the recursive invocation of the function to achieve an infinite classification, 2, the database design is arranged in a specific format, and then use MySQL to check ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.