CodeIgniter Forums
Trouble with global scope of helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Trouble with global scope of helper (/showthread.php?tid=22134)



Trouble with global scope of helper - El Forum - 08-30-2009

[eluser]Unknown[/eluser]
I couldn't find any post for my problem so that there is the problem.
I'll be thankful to anyone who can help me, because I really have no idea what to do.
I'm trying to list recursive tree and I made helper to walk recursive array with categories. But when I am trying to get the value inside the recursive function, I realize that this helper doesn't return any value and I got only main categories.

This is my category_model

Code:
$q = "
SELECT
    category_id as id,
    category_parent_id as parent_id,
      category_level as level,
      category_name_bg as name
FROM
    category
ORDER BY
    category_id, category_sort ASC
";
$query = $this->db->query($q);
if($query->num_rows()>0)
{
    $cat_id_array = array();
    $treeCategory = '';
    $rawArr = array();
    $j = 0;
    //add database values to array
    foreach($query->result() as $row)
    {
        $rawArr[$j]['id'] = $row->id;
        $rawArr[$j]['parent_id'] = $row->parent_id;
        $rawArr[$j]['name'] = $row->name;
        $rawArr[$j]['level'] = $row->level;
        $j++;
    }
    //recursive walk of array
    for($i=0; $i < count($rawArr); $i++){
        if($rawArr[$i]['parent_id'] == 0){
            $treeCategory .= '<li>'.$rawArr[$i]['name'].'</li>';
            array_push($cat_id_array, $rawArr[$i]['id']);
            $last = count($cat_id_array)-1;
            treeRecursive($cat_id_array[$last], $cat_id_array);
        }
    }
}

This is my database_helper (loaded into $autoload['model'] in config/autoload.php )

Code:
function treeRecursive($id, $cat_id_array)
{
    global $rawArr, $treeCategory;
    for($a=0; $a < count($rawArr); $a++){
        if($id == $rawArr[$a]['parent_id']){
            $treeCategory .= '<li>'.$rawArr[$a]['name'].'</li>';
            array_push($cat_id_array, $rawArr[$a]['id']);
            treeRecursive($rawArr[$a]['id'], $cat_id_array);
        }
    }
    array_pop($cat_id_array);
}

I have tested this without Code Igniter and works perfect.
Where is my mistake. Thanks in advance.


Trouble with global scope of helper - El Forum - 08-30-2009

[eluser]Unknown[/eluser]
I found solution. I don't know if this is the best but it works. Helper proved works only with 'echo' or 'print' of returned value. Therefore I moved whole loop and recursion into category_view and return the array with data (from DB) through the model method. If somebody have any other idea I'll be glad to hear it.


Trouble with global scope of helper - El Forum - 08-30-2009

[eluser]t'mo[/eluser]
Another idea is to try nested sets instead of parent/child relationships in your data (see also this article). There's a nice implementation of this concept on the wiki.