php 递归函数来获取所有子类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2398402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
recursive function to get all the child categories
提问by user253530
Here is what I'm trying to do: - i need a function that when passed as an argument an ID (for a category of things) will provide all the subcategories and the sub-sub categories and sub-sub-sub..etc. - i was thinking to use a recursive function since i don't know the number of subcategories their sub-subcategories and so on so here is what i've tried to do so far
这是我正在尝试做的: - 我需要一个函数,当作为参数传递时,ID(用于事物类别)将提供所有子类别和子子类别和子子子......等. - 我正在考虑使用递归函数,因为我不知道他们的子类别的子类别数量等等,所以这是我迄今为止尝试做的
function categoryChild($id) {
$s = "SELECT * FROM PLD_CATEGORY WHERE PARENT_ID = $id";
$r = mysql_query($s);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_array($r))
echo $row['ID'].",".categoryChild($row['ID']);
}
else {
$row = mysql_fetch_array($r);
return $row['ID'];
}
}
If i use return instead of echo, i won't get the same result. I need some help in order to fix this or rewrite it from scratch
如果我使用 return 而不是 echo,我不会得到相同的结果。我需要一些帮助来解决这个问题或从头开始重写它
回答by Bruno De Barros
I had a hard time trying to figure out your function. I think this will do what you want. It gets all the children of a category with ID $id, and also their children (thus getting the whole sub category, sub sub category effect that you wanted).
我很难弄清楚你的功能。我认为这会做你想做的。它获取 ID 为 $id 的类别的所有子项,以及它们的子项(从而获得您想要的整个子类别、子子类别效果)。
function categoryChild($id) {
$s = "SELECT ID FROM PLD_CATEGORY WHERE PARENT_ID = $id";
$r = mysql_query($s);
$children = array();
if(mysql_num_rows($r) > 0) {
# It has children, let's get them.
while($row = mysql_fetch_array($r)) {
# Add the child to the list of children, and get its subchildren
$children[$row['ID']] = categoryChild($row['ID']);
}
}
return $children;
}
This function will return:
该函数将返回:
$var = array(
'categoryChild ID' => array(
'subcategoryChild ID' => array(
'subcategoryChild child 1' => array(),
'subcategoryChild child 2' => array()
)
),
'anotherCategoryChild ID' => array() # This child has no children of its own
);
It basically returns an array with the ID of the child and an array containing the IDs of its children. I hope this is of any help.
它基本上返回一个包含子代 ID 的数组和一个包含其子代 ID 的数组。我希望这有任何帮助。
回答by Matthew
database tree to multidimensional array
<?php
function getTree($rootid)
{
$arr = array();
$result = mysql_query("select * from PLD_CATEGORY where PARENT_ID='$rootid'");
while ($row = mysql_fetch_array($result)) {
$arr[] = array(
"Title" => $row["Title"],
"Children" => getTree($row["id"])
);
}
return $arr;
}
?>
回答by binaryLV
As this has been brought up by @Pawan Sharma, I thought that I might give some answer as well.
由于这是由@Pawan Sharma 提出的,我想我也可以给出一些答案。
All given solutions suffer from common problem - they perform SQL query for each and every child. E.g., if there are 100 childs in 2nd level, then 100 queries will be done, while it can actually be done in singlequery by using where parent_id in (<list_of_ids>).
所有给定的解决方案都存在常见问题——它们为每个孩子执行 SQL 查询。例如,如果有100个孩子的在第2级,那么100个查询将完成,而实际上可以在做单的查询使用where parent_id in (<list_of_ids>)。
Sample DB:
示例数据库:
create table category (
id int auto_increment primary key,
parent_id int default null,
title tinytext,
foreign key (parent_id) references category (id)
) engine = InnoDB;
insert into category (id, parent_id, title) values
(1, null, '1'),
(2, null, '2'),
(3, null, '3'),
(4, 1 , '1.1'),
(5, 1 , '1.2'),
(6, 1 , '1.3'),
(7, 4 , '1.1.1'),
(8, 4 , '1.1.2'),
(9, 7 , '1.1.1.1');
Here's my solution:
这是我的解决方案:
/**
* @param null|int|array $parentID
*/
function getTree($parentID) {
$sql = "select id, parent_id, title from category where ";
if ( is_null($parentID) ) {
$sql .= "parent_id is null";
}
elseif ( is_array($parentID) ) {
$parentID = implode(',', $parentID);
$sql .= "parent_id in ({$parentID})";
}
else {
$sql .= "parent_id = {$parentID}";
}
$tree = array();
$idList = array();
$res = mysql_query($sql);
while ( $row = mysql_fetch_assoc($res) ) {
$row['children'] = array();
$tree[$row['id']] = $row;
$idList[] = $row['id'];
}
mysql_free_result($res);
if ( $idList ) {
$children = getTree($idList);
foreach ( $children as $child ) {
$tree[$child['parent_id']]['children'][] = $child;
}
}
return $tree;
}
With provided sample data, it does at most 5 queries, when called as getTree(null)(for all entries):
使用提供的示例数据,它最多执行 5 次查询,当被调用时getTree(null)(对于所有条目):
select id, parent_id, title from category where parent_id is null
select id, parent_id, title from category where parent_id in (1,2,3)
select id, parent_id, title from category where parent_id in (4,5,6)
select id, parent_id, title from category where parent_id in (7,8)
select id, parent_id, title from category where parent_id in (9)
When called as getTree(4), 3 queries are performed:
当调用 as 时getTree(4),执行 3 个查询:
select id, parent_id, title from category where parent_id = 4
select id, parent_id, title from category where parent_id in (7,8)
select id, parent_id, title from category where parent_id in (9)
回答by sven
function breadCrumb($id)
{
$ar = array();
$result = mysql_query("SELECT * FROM groups WHERE ParentID = '$id'");
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_object($result))
{
$ar[] = $row->DBGroupID;
$r = mysql_query("SELECT * FROM groups WHERE ParentID = '".$row->GroupID."'");
if(mysql_num_rows($r) > 0)
$ar = array_merge($ar, breadCrumb($row->GroupID, 1));
}
}
return $ar;
}
回答by Pawan Sharma
function categoryChild($id)
{
$s = "SELECT category_id,name FROM proads_categories WHERE parent_id =".$id;
$r = mysql_query($s);
$children = array();
if(mysql_num_rows($r) > 0)
{
#It has children, let's get them.
while($row = mysql_fetch_array($r))
{
#Add the child to the list of children, and get its subchildren
$children[$row['category_id']]['nam'] = $row['name'];
$arr = categoryChild($row['category_id']);
if(count($arr) > 0)
{
$children[$row['category_id']]['child'] = categoryChild($row['category_id']);
}
}
}
return $children;
}
It's perfect. If you need please try this
这是完美的。如果你需要请试试这个
回答by Vikas Sharma
<?php
require('db/dbconnect.php');
$user_id='triD-100';
$sql="select * from ajent_joining where sponser_id='".$user_id."'";
$qR=mysql_query($sql);
while($rowD=mysql_fetch_assoc($qR)){
echo $childId=$rowD["user_id"];
echo "<br/>";
categoryChild($childId);
}
function categoryChild($childId) {
$s = "select user_id from ajent_joining where sponser_id='".$childId."'";
$r = mysql_query($s);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_array($r)) {
echo $childId=$row["user_id"];
echo "<br/>";
categoryChild($childId);
}
}
}
?>
回答by Karim Slamani
Using Prestashop function :
使用 Prestashop 功能:
public function getRecursiveChildren() {
$subCategories = $this->recurseLiteCategTree();
//print_r($subCategories);
$my_tab = array();
foreach ($subCategories['children'] as $subc) {
$my_tab[] = $subc['id'];
foreach ($subc['children'] as $subc2) {
$my_tab[] = $subc2['id'];
foreach ($subc2['children'] as $subc3) {
$my_tab[] = $subc3['id'];
foreach ($subc3['children'] as $subc4) {
$my_tab[] = $subc4['id'];
}
}
}
}
$my_tab [] = $this->id;
return $my_tab;
}
this can be ameliorated using recursivity but no time for that today :'(
这可以使用递归来改善,但今天没有时间:'(

