您好我已经为joomla安装了jbusinessdirectory组件,并且我在tmpl / default.php文件中有名为mod_jbusinessdirectory的模块(这是商业列表的搜索模块)我有选择代码:(见下文)
<?php if($params->get('showCategories')){ ?>
<div class="select">
<div class="categoryic"></div>
<select name="categorySearch" class="select-styled" id="categories">
<option value="0">category</option>
<?php foreach($categories as $category){?>
<option value="<?php echo $category->id?>" <?php echo $session->get('categorySearch')==$category->id && $preserve?" selected ":"" ?> ><?php echo $category->name?></option>
<?php if(!empty($category->subcategories)){?>
<?php foreach($category->subcategories as $subCat){?>
<option value="<?php echo $subCat->id?>" <?php echo $session->get('categorySearch')==$subCat->id && $preserve?" selected ":"" ?> >-- <?php echo $subCat->name?></option>
<?php }?>
<?php }?>
<?php }?>
</select>
</div>
<?php }?>
从这段代码中我得到类别和子类别:
子类别1子类别2子类别3
主要类别2
此处截图:categories and sub categories screenshot
在helper.php中,我有从数据库中获取类别和子类别的函数
static function getMainCategories(){
$db = JFactory::getDBO();
$query = ' SELECT * FROM #__jbusinessdirectory_categories where parent_id=1 and published=1 order by name';
$db->setQuery($query);
return $db->loadObjectList();
}
static function getSubCategories(){
$db = JFactory::getDBO();
$query = ' SELECT c.* FROM #__jbusinessdirectory_categories c
inner join #__jbusinessdirectory_categories cc on c.parent_id = cc.id where c.parent_id!=1 and cc.parent_id = 1 and c.published=1
order by c.name';
$db->setQuery($query,0,1000);
$result = $db->loadObjectList();
return $result;
}
最后在modjbusinesdirectory.php文件中我有这样的PHP:
if($params->get('showCategories')){
$categories = modJBusinessDirectoryHelper::getMainCategories();
if($params->get('showSubCategories')){
$subCategories = modJBusinessDirectoryHelper::getSubCategories();
foreach($categories as $category){
foreach($subCategories as $subCat){
if($category->id == $subCat->parent_id){
if(!isset($category->subcategories)){
$category->subcategories = array();
}
$category->subcategories[] = $subCat;
}
}
}
}
}
类别和子类别表结构截图 here
我的问题是:如何制作两个选择查询而不是一个。在第一个查询中我得到主要类别,在第二个查询中我得到子类别(例如:如果我从第一个查询中选择主要类别书籍,在第二个查询中我选择子类别,它必须仅显示具有子类别的书籍儿童书籍)。
答案 0 :(得分:1)
您可以使用以下查询获取类别。
function all_cat($id='',$child_of_child='parent')
{
$CI =& get_instance();
if($id="")
{
$query = $CI->db->get_where("__jbusinessdirectory_categories",array('parent_id'=>1));
}
else
{
$query = $CI->db->get_where("__jbusinessdirectory_categories",array('parent_id'=>$id));
}
//echo $CI->db->last_query()."<br>";
$op = '';
if($query->num_rows() > 0)
{
$res = $query->result();
//pr($res);
if($child_of_child == 'child')
{
$op .= '<ul class="sub_cat mrg-top-5" style="display:none;" id="'.$id.'">';
}
else
{
$op .= '<ul class="sub_cat " id="'.$id.'">';
}
foreach($res as $r)
{
$op .= '<li><a href="'.site_url('category/search/9/1V1/'.$r->id).'" class="temp">'.ucwords($r->name).'</a>
</li>';
$op .= all_cat($r->id,$child_of_child='child');
}
return $op .= '</ul>';
}
else
{
return $op;
}
}
答案 1 :(得分:0)
看起来代码已经做了你想要的。你想要2个查询,但有2个。但是,我认为这2个查询的结果合并为1个结果,你想要将它分成2个结果。
您必须添加接受类别ID参数的新类别函数。
<?php
static function getCategoryById($id){
$id = intval( $id );
$db = JFactory::getDBO();
$query = '
SELECT *
FROM #__jbusinessdirectory_categories
WHERE
published=1
AND id= '.$id;
$db->setQuery($query);
return $db->loadObject();
}
static function getChildCategoryList($id){
$id = intval($id);
$db = JFactory::getDBO();
$query = '
SELECT c.*
FROM #__jbusinessdirectory_categories c
INNER JOIN #__jbusinessdirectory_categories cc
ON c.parent_id = cc.id
WHERE c.parent_id!=1
AND cc.id = '.$id.'
AND c.published=1
ORDER BY c.name';
$db->setQuery($query,0,1000);
$result = $db->loadObjectList();
return $result;
}
我不熟悉检索参数的Joomla代码,但如果您在某人选择主要类别时发布了该ID,则可以找到该ID。
<?php
if($params->get('showCategories')){
$categoryId = (int) $params->get('mainCategory');
$category = modJBusinessDirectoryHelper::getCategoriesById($categoryId);
$subCategories = modJBusinessDirectoryHelper::getChildCategoryList($categoryId);
}
}