旁门左道做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;
}


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

[img]/upload/attachment/125715/65218ebb-67b1-3bce-b197-f58251bd85c6.jpg[/img]
数据库实例“2快速消费”的父分类是“1金融财经”
“9服装服饰”的父分类是“5娱乐休闲”

[img]/upload/attachment/125719/2b0dbf47-d4f8-3ad4-be86-c7dfed902be4.jpg[/img]
后台打开“快速消费”分类编辑页面效果

[img]/upload/attachment/125722/9e527bf1-7bd4-3d73-8d83-c637a4a56079.jpg[/img]
后台更换单元时,分类自动随之更换
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值