旁门左道做joomla多级分类一

利用parent_id,暂时只考虑增加一级分类,而不是无限分类
单元->一级分类->二级分类->文章

1.
admin.categories.php的function editCategory($edit )里仿照section代码添加:
$category	= JRequest::getCmd( 'category', 'com_content' );


		// build the html select list for Categories
	if ( $category == 'com_content' ) {

		if (!$row->catid && JRequest::getInt('parent_id')) {
		    $row->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->setQuery( $query );
		$category = $db->loadObjectList();
		$lists['category'] = JHTML::_('select.genericlist',   $category, 'parent_id', 'class="inputbox" size="1"', 'value', 'text', $row->cateid );
	}
至此可以显示选择parent_id表单并可以保存parent_id值,
以上不用了
换为以下代码:
			foreach ($sections as $section)
		{
			$section_list[] = (int) $section->value;
			// get the type name - which is a special category
			if ($row->section) {
				if ($section->id == $row->section) {
					$contentSection = $section->text;
				}
			} else {
				if ($section->id == $row->section) {
					$contentSection = $section->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->setQuery($query);
		$cat_list = $db->loadObjectList();
  
		// Uncategorized category mapped to uncategorized section
		$uncat = new stdClass();
		$uncat->id = 0;
		$uncat->title = JText::_('Uncategorized');
		$uncat->section = 0;
		$cat_list[] = $uncat;                       

		foreach ($sections as $section)
		{
			$sectioncategories[$section->value] = array ();
			$rows2 = array ();
			foreach ($cat_list as $cat)
			{
				if ($cat->section == $section->value) {
					$rows2[] = $cat;
				}
			}
			foreach ($rows2 as $row2) {
				$sectioncategories[$section->value][] = JHTML::_('select.option', $row2->id, $row2->title, 'id', 'title');
			}
		}
		  
		$categories = array();
		foreach ($cat_list as $cat) {
			if($cat->section == $row->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->parent_id);

方法最下面修改代码为;
categories_html::edit( $row, $lists, $redirect,$sectioncategories );
将$sectioncategories的值也传入到categories_html::edit()方法里。
添加js:用于动态更换分类列表,当改变单元时,分类随之改变。
<script type='text/javascript'>
function changeCatList( listname, source, key, orig_key, orig_val ) {
var list = eval( 'document.adminForm.' + listname );
// empty the list
for (i in list.options.length) {
list.options[i] = null;
}
i = 1;
for (x in source) {
if (source[x][0] == key) {

opt = new Option();
opt.value = source[x][1];
opt.text = source[x][2];

if ((orig_key == key && orig_val == opt.value) || i == 0) {
opt.selected = true;
}
list.options[i++] = opt;
list.options[0].value = 0;
list.options[0].text = source[0][2];
}
}
list.length = i;
}
</script>
2.
admin.categories.html.php的function edit( &$row, &$lists, $redirect )里添加:
					<tr>
						<td class="key">
							<label for="category">
								<?php echo '一级'.JText::_( 'Category' ); ?>:
							</label>
						</td>
						<td colspan="2">
							<?php echo $lists['parent_id']; ?>
						</td>
					</tr>

显示父分类选项表单
添加js代码,把$sectioncategories值传给js程序。用于动态改变分类列表选项
		<script language="javascript" type="text/javascript">
		var sectioncategories = new Array;
		<?php
		$i = 0;
		foreach ($sectioncategories as $k=>$items) {
			foreach ($items as $v) {
				echo "sectioncategories[".$i++."] = new Array( '$k','".addslashes( $v->id )."','".addslashes( $v->title )."' );\n\t\t";
			}
		}
		?>


3.(这点不需要了)
在libraries/joomla/html/html/select.php里添加一个函数
	function genericlist1( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false )
	{
		if ( is_array( $arr ) ) {
			reset( $arr );
		}
		if (is_array($attribs)) {
			$attribs = JArrayHelper::toString($attribs);
		 }
		$id = $name;
		if ( $idtag ) {
			$id = $idtag;
		}
		$id		= str_replace('[','',$id);
		$id		= str_replace(']','',$id);
		$html	= '<select name="'. $name .'" id="'. $id .'" '. $attribs .'>';
		$html .= '<option value=""></option>';
		$html	.= JHTMLSelect::Options( $arr, $key, $text, $selected, $translate );
		$html	.= '</select>';
		return $html;
	}


现在只是在后台创建或修改分类页面里看起来像三级分类,逻辑上是没有三级的,以后继续研究


数据库实例“2快速消费”的父分类是“1金融财经”
          “9服装服饰”的父分类是“5娱乐休闲”


后台打开“快速消费”分类编辑页面效果


后台更换单元时,分类自动随之更换


你可能感兴趣的:(html,C++,c,PHP,C#)