PHP Unlimited classification, support cache classification tree

Source: Internet
Author: User

This is the Category List page in the background management

Contains infinite categories

Include ROOT. ' include/tree.class.php ';

declaring infinite categories

$tree = new Tree ();

Set Cache Directory

$tree->cdir = ROOT. ' cache/class/';

Read into the category cache

$tree->getcache (' class ');

Get Cache

$rootArray = $tree->nodes; Classification information is sorted in order before it is written to the cache, and can be directly output without the need to generate a classification tree again
Copy code//You need to regenerate the taxonomy tree only if you are modifying a taxonomy table in the database

Contains infinite categories

Include ROOT. ' include/tree.class.php ';

declaring infinite categories

$tree = new Tree ();

Set Cache Directory

$tree->cdir = ROOT. ' cache/class/';

Query the database, return the category ID, name, parent Class 3 fields

$db->select (' All ', ' class ', ' id,name,parent ');

Traverse the result set and press into the infinite category

while ($row = $db->record (' All '))

{

$tree->newnode ($row [' id '], $row [' Name '], (int) $row [' parent ']); The parent class ID needs to be a number

}

Generate the taxonomy tree and write to the cache

$tree->putcache (' class ');
Copy code//Another simpler way to override caching, which is to delete the

Contains infinite categories

Include ROOT. ' include/tree.class.php ';

declaring infinite categories

$tree = new Tree ();

Set Cache Directory

$tree->cdir = ROOT. ' cache/class/';

Read into the category cache

$tree->getcache (' class ');

Whether the taxonomy exists

if (Isset ($tree->nodes[$id]))

{

Generate query criteria

$condition = ' id= '. $id;

Gets the subcategory ID of the taxonomy

$childsId = $tree->getchildsid ($id); If a subcategory exists, the modified method returns a one-dimensional array with values of the IDs for each subcategory, or False if no subcategories exist

If a subcategory exists

if ($childsId)

{

If the subcategory exists, it is deleted along with the sub category

foreach ($childsId as $childId)

{

$condition. = ' or id= '. $childId; To generate a delete condition

Unload an entry in an infinite category

unset ($tree->nodes[$childId]); Delete the category information of the corresponding ID in the classification tree directly

}

}

Start deletion

$db->delete (' class ', $condition);

Delete the category entry in the infinite category

unset ($tree->nodes[$id]);

overriding an infinite category cache

$tree->putcache (' class ');

Output Deletion Success token

Exit (' OK ');

} else {

Output error message if not present

Exit (' This category does not exist! ');

}
Copy Code

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.