donk68254 2014-05-31 13:44
浏览 107
已采纳

递归PHP函数只获取第一个值

I'm having an issue with below code. I'm trying for some time now, and I believe I must be blind and I do not see something obvious. Can you point at this obvious thing?

Function ought to get all categories and it's names. Takes IDs correctly, but names only for first category and first subcategory.

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

EDIT:

EDIT2:

After changes I'm getting array like this:

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

I think it is not problem with mysql query, as in phpmyadmin it works and shows all data properly...

  • 写回答

1条回答 默认 最新

  • duanniu3385 2014-05-31 13:48
    关注

    UPDATED :

    It seems that you have only 1 level of subdirectory. Your function does this :

    1) First it fetches the name and id of the given category. 2) It uses the obtained id to fetch the name of the subcategory which has parent_id = the previously fetched id. 3) Here comes the problem. Due to recursion it again calls collect() with the id of this subcategory. But there is no subcategory for this, so it returns null.

    That is,

    1st Parent (id1) -> Subcategory(id2) -> Subcategory(id2) and so on.

    You can use this to get as many levels of categories as you want :

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

    This code is not tested. You will get an array in the following format

    $parent[parent_id] -> [name]
    
                       -> [children] -> FALSE (if subdirectories are not present)
    
                                     -> [children_id] #1 -> [name]
    
                                                         -> [children]
    
                                     -> [children_id] #2 and so on.
    

    UPDATE : you were getting only the last result, because I forgot one thing. The $temp[] array was getting overwritten every time in the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?