---
title: "旁门左道做joomla多级分类一"
author: "aiou"
date: "2009-07-16T16:28:21Z"
tags: ["C", "C#", "C++", "HTML", "PHP"]
categories: ["编程语言"]
slug: "joomla"
draft: false
lastmod: "2009-07-16T16:28:21Z"
id: 427217
url: "https://aiou.iteye.com/blog/427217"
---

利用parent\_id，暂时只考虑增加一级分类，而不是无限分类 单元-&gt;一级分类-&gt;二级分类-&gt;文章 1. admin.categories.php的function editCategory($edit )里仿照section代码添加： \[code="php"]$category = JRequest::getCmd( 'category', 'com\_content' );\[/code] 和 \[code="php"] // build the html select list for Categories if ( $category == 'com\_content' ) { if (!$row-&gt;catid && JRequest::getInt('parent\_id')) { $row-&gt;catid = JRequest::getInt('parent\_id'); } $query = 'SELECT c.id AS value, c.title AS text' . ' FROM #\_\_categories AS c' . ' WHERE c.section NOT LIKE "%com\_%"' . ' AND c.published != -2' . ' ORDER BY c.ordering' ; $db-&gt;setQuery( $query ); $category = $db-&gt;loadObjectList(); $lists\['category'] = JHTML::\_('select.genericlist', $category, 'parent\_id', 'class="inputbox" size="1"', 'value', 'text', $row-&gt;cateid ); } 至此可以显示选择parent\_id表单并可以保存parent\_id值， 以上不用了 换为以下代码： foreach ($sections as $section) { $section\_list\[] = (int) $section-&gt;value; // get the type name - which is a special category if ($row-&gt;section) { if ($section-&gt;id == $row-&gt;section) { $contentSection = $section-&gt;text; } } else { if ($section-&gt;id == $row-&gt;section) { $contentSection = $section-&gt;text; } } } $sectioncategories = array (); $sectioncategories\[-1] = array (); $sectioncategories\[-1]\[] = JHTML::\_('select.option', '-1', JText::\_( 'Select Category' ), 'id', 'title'); $section\_list = implode('\\', \\'', $section\_list); $query = 'SELECT id, title, section' . ' FROM #\_\_categories' . ' WHERE section IN ( \\''.$section\_list.'\\' )' . ' AND parent\_id = 0 ' . ' ORDER BY ordering'; $db-&gt;setQuery($query); $cat\_list = $db-&gt;loadObjectList(); // Uncategorized category mapped to uncategorized section $uncat = new stdClass(); $uncat-&gt;id = 0; $uncat-&gt;title = JText::\_('Uncategorized'); $uncat-&gt;section = 0; $cat\_list\[] = $uncat; foreach ($sections as $section) { $sectioncategories\[$section-&gt;value] = array (); $rows2 = array (); foreach ($cat\_list as $cat) { if ($cat-&gt;section == $section-&gt;value) { $rows2\[] = $cat; } } foreach ($rows2 as $row2) { $sectioncategories\[$section-&gt;value]\[] = JHTML::\_('select.option', $row2-&gt;id, $row2-&gt;title, 'id', 'title'); } } $categories = array(); foreach ($cat\_list as $cat) { if($cat-&gt;section == $row-&gt;section) $categories\[] = $cat; } $categories\[] = JHTML::\_('select.option', '0', JText::\_( 'Select Category' ), 'id', 'title'); $lists\['parent\_id'] = JHTML::\_('select.genericlist', $categories, 'parent\_id', 'class="inputbox" size="1"', 'id', 'title', $row-&gt;parent\_id);\[/code] 方法最下面修改代码为; categories\_html::edit( $row, $lists, $redirect,$sectioncategories ); 将$sectioncategories的值也传入到categories\_html::edit（）方法里。 添加js：用于动态更换分类列表，当改变单元时，分类随之改变。 2. admin.categories.html.php的function edit( &$row, &$lists, $redirect )里添加： \[code="php"] : \[/code] 显示父分类选项表单 添加js代码，把$sectioncategories值传给js程序。用于动态改变分类列表选项 \[code="html"]