PHP implementation Query Multilevel classification of program code

Source: Internet
Author: User

The database structure design of the algorithm is the simplest. Category A field ID in the table, a field FID (parent ID). This allows you to determine the level of content based on where id = FID, using recursion to the top

Let's find the array first.

The code is as follows Copy Code
<?php
$a = Array (
' Aaaaaa ' => Array (
' Aaaaaa ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' BBBBBB ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' CCCCCC ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
),
' BBBBBB ' => Array (
' Aaaaaa ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' BBBBBB ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' CCCCCC ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
),
' CCCCCC ' => Array (
' Aaaaaa ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' BBBBBB ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
' CCCCCC ' => Array (
' 111111 ',
' 222222 ',
' 333333 '
),
),
);
foreach ($a as $k => $v) {
echo $k. " <br> ";
if (Is_array ($v)) {
foreach ($v as $key => $val) {
echo "". $key. " <br> ";
//     }
if (Is_array ($val)) {
foreach ($val as $kkk => $vall) {
echo "". $vall. " <br> ";
}
}
}
echo "<br>";
}


Code ******/for/*******mysql query infinite level classification
/***
$sql = "Select A.title as Big, b.title as small
From Largetitle as a left JOIN Smalltitle as B on A.id=b.largeid ";

$a = array ();

$r = mysql_query ($sql);

while ($arr = Mysql_fetch_array ($r)) {
$a [$arr [' big ']] = $arr [' small '];
}
***/
?>

All right, let's read the database and then read it recursively.

Categories, such as category, fields have Id,parentid,title

The code is as follows Copy Code

Inquire

$dsql->setquery ("SELECT * from category ORDER by SortOrder ASC");
$dsql->execute (' parentlist ');
$array = Array ();
$parentlist = Array ();
while ($rs = $dsql->getobject (' parentlist '))
{
if ($rs->parentid = = 0)
{
$parentlist [$rs->id] = (array) $rs;
}
Else
{
$array [$rs->id] = (array) $rs;
}
}
$parentlist = Cat_options ($parentlist, $array); We're asking for an array of results

An array of $list parent categories

$array is an array of all categories except the parent category

Function cat_options (& $list,& $array)
{
foreach ($list as $key => $arr)
{
foreach ($array as $k => $value)
{
if ($value [' parentid '] = = $arr [' id '])
{
$list [$key] [' children '] = $value;
Unset ($array [$k]);
}
}
}
foreach ($list as $key => $arr)
{
if (Is_array ($arr [' Children ']) && count ($arr [' Children ']) > 0)
{
$list [$key] [' children '] = cat_options ($list [$key] [' Children '], $array);
}
}
return $list;
}

Other methods, set the FID field type to varchar, the parent class ID is concentrated in this field, separated by symbols, such as: 1,3,6
This makes it easier to get the IDs of the superior categories, and when querying the information under the classification,
You can use: SELECT * from category WHERE pid like "1,3%".

  code is as follows copy code

--
--table's structure ' category '
--
CREATE table IF not EXISTS ' category ' (
' id ' int (one) not NULL Auto_increme NT,
' type ' int (one) not null COMMENT ' 1 for Article Type 2 for product Type 3 for download type ',
' title ' varchar NOT NULL,
' lft ' int (one) not N ull,
' RGT ' int (one) not null,
' lorder ' int (one) not null COMMENT ' sort ',
' create_time ' int (one) not NULL,
P Rimary KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=10;
--
--Export the data in the table ' category '
--
INSERT into ' category ' (' id ', ' type ', ' title ', ' LfT ', ' rgt ', ' lorder ', ' CR Eate_time ') VALUES
(1, 1, ' Top columns ', 1, 1, 1261964806),
(2, 1, ' Company Profile ', X, M, 1264586212),
(3, 1, ' News ', A, 1264586226,
(4, 2, ' company Products ', ten, One, 1264586249),
(5, 1, ' Honors ', 8, 9, M, 1264586270),
( 6, 3, ' data downloads ', 6, 7, 1264586295,
(7, 1, ' recruitment ', 4, 5, 1264586314),
(8, 1, ' Message board ', 2, 3, 50, 1264586884),
(9, 1, ' president ', 1267771951, n);

The

/**
* Displays the tree, displaying all nodes.
* 1, get the left and right values of the root node first (the default root is the title of the "top-level directory").
* 2, query the left and right values in the root node of the value range of records, and sorted by the left value.
* 3, if the right value of this record is greater than the previous record of the right value is a subcategory, the output time with spaces.
* @return Array
**/
function Display_tree () {

Get the value to the left and right of root
$arr _LR = $this->category->where ("title = ' top Column '")->find ();
Print_r ($arr _LR);
if ($arr _lr) {
$right = Array ();
$arr _tree = $this->category->query ("Select ID, type, title, RGT from category WHERE LfT >=". $arr _lr[' LfT ']. " and LfT <= ". $arr _lr[' RGT ']." ORDER by LfT ");
foreach ($arr _tree as $v) {
if (count ($right)) {
while ($right [count ($right)-1] < $v [' RGT ']) {
Array_pop ($right);
}
}
$title = $v [' title '];
if (count ($right)) {
$title = ' | | '. $title;
}
$arr _list[] = array (' ID ' => $v [' id '], ' type ' => $type, ' title ' => str_repeat (', Count ($right)). $title, ' name ' = > $v [' title ']];
$right [] = $v [' RGT '];
}
return $arr _list;
}
}

All right, so long as this all sorts of categories can be queried at once, instead of recursion.

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.