无法检索子类别 [英] Unable to retrieve the sub categories

查看:123
本文介绍了无法检索子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经尝试制作类别和子类别,并尝试调用子类别 - sub cat name ,因为我把类别和子类别放在同一个表是使用嵌套选项。

Hello I have tried to make categories and sub categories and trying to call sub categories as --sub cat name because I placed the category and sub category in the same table is used nested option. But, that is not working for me.

任何人都可以帮我解决这个类别,但无法获得子类别

Can anyone help me out this this categories are getting but unable to get sub categories

<select name="category" class="dropdown">
    <?php
        $query1 = $this->db->query('SELECT * FROM categories');
        foreach($query1->result() as $cat_name) {
            if($cat_name->cat_name == $cat_name->parent)
                echo "<option>". $cat_parent = $cat_name->cat_name."</option>";
                $query2 = $this->db->query("SELECT * FROM categories WHERE parent = '$cat_parent '");
                foreach($query2->result() as $sub_cat) {
                    if($sub_cat->cat_name != $sub_cat->parent) {
                        echo "<option> --" . $sub_cat->cat_name . "</option>";
                    }
                }
        }
    ?>
</select>


推荐答案

尝试以下功能,在核心php所以,你必须实现自己的codeigniter

Try following function which should work for you and it is in core php so,you have to implement yourself in to codeigniter

function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {
global $con;
if (!is_array($user_tree_array))
$user_tree_array = array();

$sql = "SELECT * FROM `location` WHERE 1 AND `parent_id` = $parent ORDER  BY id ASC";
$result=$con->query($sql);

if (mysqli_num_rows($result) > 0)
{
  $user_tree_array[] = "<ul>";
  while ($row =$result->fetch_object())
  {
   $user_tree_array[] = "<li>". $row->name."</li>";
   $user_tree_array = fetchCategoryTreeList($row->id, $user_tree_array);
  }
  $user_tree_array[] = "</ul><br/>";
}
return $user_tree_array; 
}

并将其命名为

$res = fetchCategoryTreeList();
foreach ($res as $r)
{
  echo  $r;
}

希望它会帮助你

这篇关于无法检索子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆