gpt4 book ai didi

javascript - 逻辑选择类别和子类别(php、joomla、javascript、ajax)

转载 作者:IT老高 更新时间:2023-10-28 23:58:23 25 4
gpt4 key购买 nike

你好,我已经为 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
  • 子类别 1 子类别 2 子类别 3

  • 主要类别2

  • 子类别 1 子类别 2 子类别 3

此处截图: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

我的问题是:如何进行两个选择查询而不是一个。在第一个查询中我得到主要类别,在第二个查询中我得到子类别(例如:如果我从第一个查询中选择主类别书籍,在第二个查询中我选择子类别,它必须只显示子类别的书籍童书)。

最佳答案

您可以使用以下查询获取类别。

 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;
}
}

关于javascript - 逻辑选择类别和子类别(php、joomla、javascript、ajax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com