1

I am new to CI . I had a function to access a list of data as category. The function was like this:

      public function get_category_tree()
{
    $this->ci->db->where('is_display','1');
    $query = $this->ci->db->get('product_categories');

    if ($query->num_rows() > 0) 
    {

        foreach($query->result() as $cat)
        {
            if($cat->parent_id=='0'){
                //category
                $categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => '');
            }else{
                //subcategory;
                $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
            }
        }
        return $categories_arr;
    }               
    return false;
}

this function was defined in general library and i accessed it from the controller like this:

    $this->data['category_tree'] = $this->general->get_category_tree();

And this used to give me this result:

    Array
  (
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] => 
    )
    Array
    (
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
    (
        [0] => Array
            (
                [id] => 78
                [parent_id] => 32
                [name] => Africa
            )

        [1] => Array
            (
                [id] => 79
                [parent_id] => 32
                [name] => Asia
            )

        [2] => Array
            (
                [id] => 80
                [parent_id] => 32
                [name] => Australia
            )

        [3] => Array
            (
                [id] => 81
                [parent_id] => 32
                [name] => Br Comm Other
            )

        [4] => Array
            (
                [id] => 82
                [parent_id] => 32
                [name] => Canada
            )

        [5] => Array
            (
                [id] => 83
                [parent_id] => 32
                [name] => Europe
            )

        [6] => Array
            (
                [id] => 84
                [parent_id] => 32
                [name] => Latin America
            )

        [7] => Array
            (
                [id] => 85
                [parent_id] => 32
                [name] => Middle East
            )

        [8] => Array
            (
                [id] => 86
                [parent_id] => 32
                [name] => Publications
            )

        [9] => Array
            (
                [id] => 87
                [parent_id] => 32
                [name] => Topical & Specialty
            )

        [10] => Array
            (
                [id] => 88
                [parent_id] => 32
                [name] => UK (Great Britain)
            )

        [11] => Array
            (
                [id] => 89
                [parent_id] => 32
                [name] => United States
            )

        [12] => Array
            (
                [id] => 90
                [parent_id] => 32
                [name] => Worldwide
            )

    )

   )

Now i upgraded my php to 7+, now the line

    $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);

did not work and gave php error and i simply removed the [] and it became

     $categories_arr[$cat->parent_id]['subcat'] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);

Now when i print_it it gives me this result:

    Array
   (
  [id] => 19
  [parent_id] => 0
  [name] => DVDs & Movies
  [subcat] => 
   )
   Array
  (
  [id] => 32
  [parent_id] => 0
 [name] => Stamps
[subcat] => Array
    (
        [id] => 90
        [parent_id] => 32
        [name] => Worldwide
    )

  )

Only the last array of the sub array. How can i solve it. I searched a lot about the upgrade and fix for it but couldn't garb anything useful. Can anyone please help. Thanks in advance

3
  • what the error ? Commented Jun 14, 2017 at 9:35
  • @JYoThI Fatal error: Uncaught Error: [] operator not supported for strings in E:\xampp\htdocs\tshirt_design\application\libraries\general.php:209 Stack trace: #0 Commented Jun 14, 2017 at 9:38
  • 1
    you defined subcat as string . so now your using array syntax on string so .it will give this error so . define the subcat as array . as @Mohmad mentioned below . Commented Jun 14, 2017 at 9:47

1 Answer 1

2

Because you define subcat as string by 'subcat' => '' in defining $categories_arr[$cat->id]. Use this:

public function get_category_tree()
{
    $this->ci->db->where('is_display','1');
    $query = $this->ci->db->get('product_categories');

    if ($query->num_rows() > 0) 
    {

        foreach($query->result() as $cat)
        {
            if($cat->parent_id=='0'){
                //category
                $categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => array());
            }else{
                //subcategory;
                $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
            }
        }
        return $categories_arr;
    }               
    return false;
}
Sign up to request clarification or add additional context in comments.

6 Comments

WOW!! that worked but how can i check if the $category['subcat'] array is empty
You can use if(!$category['subcat']) or if(empty($category['subcat'])) to check array is empty.
okay . Can you please tell me what is $categories_arr[$cat->id] and $categories_arr[$cat->parent_id] ?
is that defining array?
$categories_arr[$cat->id] = define value of $categories_arr array element that has $cat->id index. So if not exists, create it and if exists, overwrite it.
|

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.