© 2005-2020 万博体育官网模板堂 版权所有,并保留所有权利。
专注模板制作、二次开发服务!
商品详细页调用当前子分类下的所有分类
先看效果:
操作方法:
第一:在goods.php的最下面添加一个function
//获取相关栏目的分类
function
get_categorys_info(https://www.ecshop.co/files/
$parent_id
)
{
https://www.ecshop.co/files/
$sql
=
"SELECT * FROM "
. https://www.ecshop.co/files/
$GLOBALS
[
'ecs'
]->table(https://www.ecshop.co/files/
'category'
) .
" WHERE parent_id="
.https://www.ecshop.co/files/
$parent_id
.
" ORDER BY sort_order asc"
;
https://www.ecshop.co/files/
$res
= https://www.ecshop.co/files/
$GLOBALS
[
'db'
]->getAll(https://www.ecshop.co/files/
$sql
);
https://www.ecshop.co/files/
$categorys
=
array
();
foreach
(https://www.ecshop.co/files/
$res
AS https://www.ecshop.co/files/
$idx
=> https://www.ecshop.co/files/
$row
){
https://www.ecshop.co/files/
$categorys
[https://www.ecshop.co/files/
$idx
][
'id'
] = https://www.ecshop.co/files/
$row
[
'cat_id'
];
https://www.ecshop.co/files/
$categorys
[https://www.ecshop.co/files/
$idx
][
'url'
] = build_uri(https://www.ecshop.co/files/
'category'
,
array
(
'cid'
=>https://www.ecshop.co/files/
$row
[
'cat_id'
]), https://www.ecshop.co/files/
$row
[
'cat_name'
]);
https://www.ecshop.co/files/
$categorys
[https://www.ecshop.co/files/
$idx
][
'name'
] = https://www.ecshop.co/files/
$row
[
'cat_name'
];
}
return
$categorys
;
}
第二:在然后我们需要获取这个栏目的父级,在顶部获取到这个栏目的父级
$cat_id
= https://www.ecshop.co/files/
$db
->getOne(
"SELECT cat_id FROM "
. https://www.ecshop.co/files/
$ecs
->table(https://www.ecshop.co/files/
'goods'
) .
" WHERE goods_id = '$goods_id' "
);
$parent_id
= https://www.ecshop.co/files/
$db
->getOne(
"SELECT parent_id FROM "
. https://www.ecshop.co/files/
$ecs
->table(https://www.ecshop.co/files/
'category'
) .
" WHERE cat_id = '$cat_id' "
);
第三:那么我们就拿$parent_id这个变量获取
$smarty
->assign(
'categorys_info'
, get_categorys_info(https://www.ecshop.co/files/
$parent_id
));
https://www.ecshop.co/files/
$smarty
->assign(
'cat_id'
, https://www.ecshop.co/files/
$cat_id
);
把他添加到smarty模板去赋值。
最后我们到我们自己的模板里面把他循环出来
{foreach from=$categorys_info item=r}
{if $cat_id!=$r[id]}
<
li
><
a
href
=
"{$r.url}"
title
=
"{$r.name}"
>{$r.name}</
a
></
li
>
{/if}
{/foreach}
很多人问为什么还要加个if去判断呢,因为你循环出来的是全部,你要把本身自己的栏目T出去。