---
title: "旁门左道做joomla多级分类二"
author: "aiou"
date: "2009-07-17T14:38:14Z"
tags: ["Access", "C", "C#", "C++", "PHP"]
categories: ["Web前端"]
slug: "joomla"
draft: false
lastmod: "2009-07-17T14:38:14Z"
id: 427851
url: "https://aiou.iteye.com/blog/427851"
---

下面的工作是将分类列表页的排布成一级分类下面紧跟其所属二级分类，显示出父子关系，如： 一级分类一 \_\_\_\_二级分类一 \_\_\_\_二级分类二 一级分类二 \_\_\_\_二级分类三 \_\_\_\_二级分类四 。。。 还是关系到两个文件： admin.categorise.html.php 显示文件 admin.categorise.php 查询文件 分别与function show( &$rows, $section, $section\_name, &$page, &$lists, $type ) 和function showCategories( $section, $option )有关 1.admin.categorise.php的function showCategories( $section, $option )是原始文件中的查询，会把所有的一级、二级分类查询出来并显示，我们需要的是某一级分类下的二级分类都紧跟在该一级分类下，所以原始的查询不会做到。 我的处理方法是先把所有的一级分类查询出来，在输出某一级分类时调用新添加的方法，将该分类的子分类查出来并按一定格式输出，就可以了。 所以把admin.categorise.php的function showCategories( $section, $option )的查询语句添加一个限定条件‘ AND c.parent\_id = 0’以便只查询一级分类。 2.编写新方法：仿照function showCategories( $section, $option )编写查询子分类方法 \[code="php"]function showSubCat($parent\_id, $section, $option){ global $mainframe; $db =& JFactory::getDBO(); $filter\_order = $mainframe-&gt;getUserStateFromRequest( $option.'.filter\_order', 'filter\_order', 'c.ordering', 'cmd' ); $filter\_order\_Dir = $mainframe-&gt;getUserStateFromRequest( $option.'.filter\_order\_Dir', 'filter\_order\_Dir', '', 'word' ); $filter\_state = $mainframe-&gt;getUserStateFromRequest( $option.'.'.$section.'.filter\_state', 'filter\_state', '', 'word' ); $sectionid = $mainframe-&gt;getUserStateFromRequest( $option.'.'.$section.'.sectionid', 'sectionid', 0, 'int' ); $search = $mainframe-&gt;getUserStateFromRequest( $option.'.search', 'search', '', 'string' ); $search = JString::strtolower( $search ); $section\_name = ''; if ($filter\_order == 'c.ordering'){ $order = ' ORDER BY z.title, c.ordering'; } else { $order = ' ORDER BY '.$filter\_order.' '. $filter\_order\_Dir.', z.title, c.ordering'; } if ( $sectionid &gt; 0 ) { $filter = ' AND c.section = '.$db-&gt;Quote($sectionid); } else { $filter = ''; } if ( $filter\_state ) { if ( $filter\_state == 'P' ) { $filter .= ' AND c.published = 1'; } else if ($filter\_state == 'U' ) { $filter .= ' AND c.published = 0'; } } if ($search) { $filter .= ' AND LOWER(c.title) LIKE '.$db-&gt;Quote( '%'.$db-&gt;getEscaped( $search, true ).'%', false ); } $query = 'SELECT c.\*, c.checked\_out as checked\_out\_contact\_category, g.name AS groupname, u.name AS editor' . ' , z.title AS section\_name' . ' FROM #\_\_categories AS c' . ' LEFT JOIN #\_\_users AS u ON u.id = c.checked\_out' . ' LEFT JOIN #\_\_groups AS g ON g.id = c.access' . ' LEFT JOIN #\_\_sections AS z ON z.id = c.section' . ' WHERE c.section NOT LIKE "%com\_%"' . 'AND c.parent\_id = '.$parent\_id . $filter . ' AND c.published != -2' . ' GROUP BY c.id' . $order; $db-&gt;setQuery( $query ); $rows = $db-&gt;loadObjectList(); if ($db-&gt;getErrorNum()) { echo $db-&gt;stderr(); return; } return $rows; }\[/code] 这样数据处理部分就结束了，剩下的就是显示的问题。 3.admin.categorise.html.php 的function show( &$rows, $section, $section\_name, &$page, &$lists, $type )是显示分类列表的方法。对它进行改造。 找到循环输出条目的开始处： \[code="php"] for ($i=0, $n=count( $rows ); $i &lt; $n; $i++) { $row = &$rows\[$i];\[/code] 下添加： \[code="php"]if(file\_exists(dirname(\_\_FILE\_\_).DS.'admin.categories.php')){ include\_once(dirname(\_\_FILE\_\_).DS.'admin.categories.php'); $row1 = showSubCat($row-&gt;id, $section, $option); }\[/code] 将查询子分类的方法引入，并得到结果$row1 然后找到该一级分类输出结束处添加子分类的输出： \[code="php"] foreach($row1 as $key=&gt; $ro){ JFilterOutput::objectHtmlSafe($ro); $ro-&gt;sect\_link = JRoute::\_( 'index.php?option=com\_sections&task=edit&cid\[]='. $ro-&gt;section ); $link = 'index.php?option=com\_categories§ion='. $section .'&task=edit&cid\[]='. $ro-&gt;id .'&type='.$type; $access = JHTML::\_('grid.access', $ro, $i ); $checked = JHTML::\_('grid.checkedout', $ro, $i ); $published = JHTML::\_('grid.published', $ro, $i ); ?&gt; "&gt; getRowOffset( $i ); ?&gt; get ('id'), $ro-&gt;checked\_out ) ) { if($ro-&gt;parent\_id !=0) echo "——".$ro-&gt;title; else echo $ro-&gt;title; } else { ?&gt; [parent\_id !=0) echo "——".$ro-&gt;title; else echo $ro-&gt;title; ?&gt;](%3C?php%20echo%20JRoute%3A%3A_%28%20%24link%20%29%3B%20%3F%3E) orderUpIcon( $i, ($ro-&gt;section == @$rows\[$i-1]-&gt;section), 'orderup', 'Move Up', $ordering ); ?&gt; orderDownIcon( $i, $n, ($ro-&gt;section == @$rows\[$i+1]-&gt;section), 'orderdown', 'Move Down', $ordering ); ?&gt; class="text\_area" style="text-align: center" /&gt; [section\_name; ?&gt;](%3C?php%20echo%20%24ro-%3Esect_link%3B%20%3F%3E "<?php echo JText::_( 'Edit Section' ); ?>") active; ?&gt; trash; ?&gt; id; ?&gt;