在PHP中循环从类别到子类别 [英] Loop from category to sub category in PHP

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

问题描述

我们的结构如下图所示:

We are having a structure like the way you are seeing in below screenshot:

我们的数据库结构如下:

And our database structure looks like this:

这里的数据库表PID是父ID,其中0 =父ID。其余的PID是同一个id的父id。

Here in database table "PID" is the Parent ID in which 0 = Parent ID. And rest PID is the parent id of same id.

现在我们试图获取Brand或Footware的所有子类别,因此它应显示所有子类别和子子-category)该树下的子类别详细信息。这可以通过PHP吗?任何循环或任何方式?

Now we are trying to get all the sub category of "Brand" Or "Footware" so it should show all (sub-sub-category and sub-sub-sub-category) the sub category details under that tree. Can this be done through PHP? any loop or any way?

谢谢!

推荐答案

试试这个

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

这篇关于在PHP中循环从类别到子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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