0

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
  • Your while ($row) loop should loop forever, since it never fetches a new row. Commented May 31, 2014 at 13:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I changed this, and the result is the same. I will post array which I get in my question.
@codelame if you can update your answer with the logic behind your recursive function, then I can help you quicker. What are you trying to achieve?
I'm trying to get category tree ids and names. Function gets ids, but only first names, as you can see on array updated in my question. Sorry - I thought it's obvious what I'm trying to achieve :)
thank you very much for your time :) I can't test it right now, but I hope it will work. About levels - I try to write function which will not need to know how deep it should go. Should work for 1 category and subcategories only, and should work for 10 levels :) This is my idea :) Should go as deep as it can.
@codelame I see. Then I will update the answer according to your needs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.