tags:

views:

67

answers:

3

What is the meaning of the following line:
$data[0][$row->parentid]['children'][$row->id] = $row->name

From the function:

function getCategoriesNav(){
    $data = array();

    $this->db->select('id,name,parentid');
    $this->db->where('status', 'active');
    $this->db->orderby('parentid','asc');
    $this->db->orderby('name','asc');
    $this->db->groupby('parentid,id');
    $Q = $this->db->get('categories');
    if ($Q->num_rows() > 0){
        foreach ($Q->result() as $row){
            if ($row->parentid > 0){
                $data[0][$row->parentid]['children'][$row->id] = $row->name;
            }
            else{
                $data[0][$row->id]['name'] = $row->name;
            }
        }
    }
    $Q->free_result(); 
    return $data; 
}

Where do the children come from, and what is the meaning of:
$row->parentid or $row->name?
I don't have any field 'children' in my table, and it's not declared any where. Please help

Thank you in advance, Mehdy

A: 

sorry if i didnt explain the question clearly . my question was what is the meaning of the following line in this function $data[0][$row->parentid]['children'][$row->id] = $row->name , from where this children come from , and what actual mean by $row->parentid or $row->name , i dont have any field 'children' in my table , and its not declared any where , please help me .

thank you mehdy

Mehdy
Perhaps you should ask one question at a time. The line of code you have there is constructing a hierarchical structure for rows from your dataset. I'll update my answer.
Greg K
+1  A: 

Read the code. If the row from categories has a parent ID, child IDs are added to the array under the parent ID's children element. This is simply taking the result set and converting it into a hierarchy data structure which eliminates repetition in places and makes it easier to traverse.

Update:

Specifically, what is the following line of code doing:

$data[0][$row->parentid]['children'][$row->id] = $row->name;

Your function is looping through your query result set and checking if the current row specifies a parent ID. If it does it append a hash to your array of arrays (your hierarchy).

foreach ($Q->result() as $row) {

Your query result is a collection of row objects. Each object appears to have an attribute for each column being selected in your SQL query. So for your SELECT fields:

$this->db->select('id,name,parentid');

We have:

$row->id
$row->name
$row->parentid

Going back to the original line of code:

$data[0][$row->parentid]['children'][$row->id] = $row->name;

This line is constructing a structure like this:

Array (
    [0] => Array ( // $data[0]
        [400] => Array ( // $data[0][$row->parentid]
            [children] => Array ( // $data[0][$row->parentid]['children']
                [53] => Animal // these are from [$row->id] = $row->name;
                [54] => Mineral
                [55] => Vegetable
            )
        )

        [401] => Array (
            [children] => Array (
                [4] => Wood
                [6] => Metal
                [2] => Plastic
            )
        )
    )
)

You could simplify this structure slightly by removing the first wrapper array $data[0]. So you'd change the code like so:

$data[$row->parentid]['children'][$row->id] = $row->name;

This changes the structure like so:

Array (
    [400] => Array ( // $data[$row->parentid]
        [children] => Array ( // $data[$row->parentid]['children']
            [53] => Animal // these are from [$row->id] = $row->name;
            [54] => Mineral
            [55] => Vegetable
        )
    )

    [401] => Array (
        [children] => Array (
            [4] => Wood
            [6] => Metal
            [2] => Plastic
        )
    )
)

I hope this clarifies the code for you.

Greg K
A: 

It add a value in a array namely $row->name with the ID $row->id

So what it does is adding a child row to the parent with the ID that is in $row->parentid at that moment. It loops through the loop and add child's to the parent if the parent has child's

RJD22