Php+mysql implementation of infinite classification examples, mysql instance
In this paper, the method of php+mysql realization of infinite classification is described. Share to everyone for your reference. The specific analysis is as follows:
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 infinite classification;
2, database design through a specific format to arrange, and then use the MySQL query key functions: Concat, the program implementation is relatively simple, first of all 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, and then click on the PHP news, we can find out, that is, we can go through the grandfather class level down to find, in turn, we just know a subclass of the parent class, you can find it out, This allows us to design a database with the ability to design a field of the parent class ID to implement an infinitely classified function.
The database code is as follows:
Here we build a table "class"
Copy the Code code as follows: CREATE TABLE ' class ' (
' id ' int (one) not NULL auto_increment COMMENT ' class ID ',
' f_id ' int (one) not NULL COMMENT ' parent id ',
' Name ' varchar collate gbk_bin not NULL COMMENT ' class name ',
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=gbk collate=gbk_bin auto_increment=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.
Copy the code as follows: INSERT into ' class ' (' id ', ' f_id ', ' name ') VALUES (1, 0, ' News ');//id This field is automatically grown and can be written without a value.
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.
Copy the code as follows: 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.
Copy the code as follows: 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;
Copy the code as follows: 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.
Copy the code as follows: 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 the various categories in the database, then how to list the various categories?
The PHP instance code is as follows:
Copy CodeThe code is as follows: <?php
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 fields Id,parentid,name, the algorithm is also very simple recursion, used to be very silly recursion, it should be said extremely silly, because in the recursion by querying the data table to obtain all of the subclass, the recent enlightened, think of a person on earth can get the method, the following is the code, a class, the code is as follows :
Copy CodeThe code is as follows: <?php
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 ' = ' "one column II '),
3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '),
);*/
/**
* Output Structure
* @var Array
*/
var $tree = array ();
/**
* Depth of tree recursion
* @var int
*/
var $deep = 1;
/**
* Create a tree-shaped modifier symbol
* @var Array
*/
var $icon = array (' │ ', ' ├ ', ' └ ');
/**
* Create a subordinate tree structure with the specified ID
* @param int $rootid to get the ID of the tree structure
* @param prefix used in string $add recursion
* @param bool $parent _end Identify if the parent class is the last
*/
function 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 = $child [' 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, undoubtedly greatly improve the efficiency of the program, the use of code is very simple.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/943415.html www.bkjia.com true http://www.bkjia.com/PHPjc/943415.html techarticle Php+mysql Implementation of infinite classification examples, mysql example of this article describes the Php+mysql implementation of infinite classification method. Share to everyone for your reference. The specific analysis is as follows: 1, Database ...