【问题标题】:Recursive PHP function gets only first values递归PHP函数仅获取第一个值
【发布时间】:2014-07-21 03:59:16
【问题描述】:

下面的代码有问题。我现在正在尝试一段时间,我相信我一定是盲人,我看不到明显的东西。你能指出这个显而易见的事情吗?

函数应该获取所有类别及其名称。正确获取 ID,但名称仅用于第一个类别和第一个子类别。

function collect($id, $current = null) {
    global $db_1;

    $s = "SELECT category.id as id, category_i18n.name as name from category,category_i18n WHERE category.parent_id= '$id' AND category.visible = '1' AND category_i18n.culture='en' AND category.id = category_i18n.id ORDER BY category.order_asc ASC";
    $r = mysql_query($s, $db_1);
    $row = mysql_fetch_array($r);
    $children = array();
    if (mysql_num_rows($r) > 0) {
        while ($row) {
            $children['name'] = $row['name'];
            $children[$row['id']] = collect($row['id']);
        }
    }
else
    {
            $children['name'] = $row['name'];
    }

    return $children;
}

编辑:

EDIT2:

更改后我得到这样的数组:

array(3) {
  ["name"]=>
  string(9) "Cat"
  ["id"]=>
  string(5) "10404"
  ["children"]=>
  array(3) {
    ["name"]=>
    string(10) "Subcat"
    ["id"]=>
    string(5) "10410"
    ["children"]=>
    bool(false)
  }
}

我认为 mysql 查询没有问题,因为在 phpmyadmin 中它可以正常工作并正确显示所有数据...

【问题讨论】:

  • 您的 while ($row) 循环应该永远循环,因为它永远不会获取新行。

标签: php recursion nested iteration


【解决方案1】:

更新:

您似乎只有 1 级子目录。你的功能是这样做的:

1) 首先它获取给定类别的名称和 ID。 2) 它使用获得的 id 来获取具有 parent_id = 先前获取的 id 的子类别的名称。 3) 问题来了。由于递归,它再次使用该子类别的 id 调用 collect()。但是这个没有子类,所以返回null。

也就是说,

1st Parent (id1) -> Subcategory(id2) -> Subcategory(id2) 等等。

您可以使用它来获得任意级别的类别:

function collect($id, $current = null) {
    global $db_1;

    $s = "SELECT category.id as id, category_i18n.name as name from category,category_i18n WHERE category.parent_id= '$id' AND category.visible = '1' AND category_i18n.culture='en' AND category.id = category_i18n.id ORDER BY category.order_asc ASC";
    $r = mysql_query($s, $db_1);
    $temp = array();
    if(mysql_num_rows($r) > 0)
    {
        while($row = mysql_fetch_array($r))
        { 
            $temp[$row['id']]['name'] = $row['name'];
            $temp[$row['id']]['children'] = collect($row['id']); //Get further children (Recursive)
            if(!$temp[$row['id']]['children']) {continue;} // If FALSE is returned, means that this category has no sub categories. Move onto next one.
        }
    }
    else {return FALSE;} //No rows present, return FALSE

    return $temp;
}

此代码未经测试。你会得到一个如下格式的数组

$parent[parent_id] -> [name]

                   -> [children] -> FALSE (if subdirectories are not present)

                                 -> [children_id] #1 -> [name]

                                                     -> [children]

                                 -> [children_id] #2 and so on.

更新:你只得到了最后一个结果,因为我忘了一件事。 $temp[] 数组每次在循环中都会被覆盖。

【讨论】:

  • 我改了这个,结果还是一样。我将发布我在问题中得到的数组。
  • @codelame 如果您可以使用递归函数背后的逻辑更新您的答案,那么我可以更快地帮助您。你想达到什么目的?
  • 我正在尝试获取类别树 ID 和名称。函数获取 id,但只有名字,正如您在我的问题中更新的数组中看到的那样。抱歉 - 我认为我想要达到的目标很明显:)
  • 非常感谢您抽出宝贵时间 :) 我现在无法测试它,但我希望它会起作用。关于级别 - 我尝试编写不需要知道它应该有多深的函数。应该只适用于 1 个类别和子类别,并且应该适用于 10 个级别 :) 这是我的想法 :) 应该尽可能深入。
  • @codelame 我明白了。然后我会根据你的需要更新答案。
猜你喜欢
  • 2021-08-04
  • 2020-12-17
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
相关资源
最近更新 更多