Welcome Guest, Not a member yet? Register   Sign In
Trouble with global scope of helper
#1

[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.


Messages In This Thread
Trouble with global scope of helper - by El Forum - 08-30-2009, 02:15 PM
Trouble with global scope of helper - by El Forum - 08-30-2009, 02:55 PM
Trouble with global scope of helper - by El Forum - 08-30-2009, 11:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB