node_man
node_man

Reputation: 1449

How to create tree view hierarchy in php codeigniter?

I have a array like this :

Array
(
    [0] => Array
        (
            [id] => 1
            [child_name] => "Emma"
            [parent_name] => "Mr Brown"
        )

    [1] => Array
        (
           [id] => 2
            [child_name] => "John"
            [parent_name] => "Mr Brown"
        )

    [2] => Array
        (
            [id] => 3
            [child_name] => "Joseph"
            [parent_name] => "Mr Thomas"
        )

    [3] => Array
        (
            [id] => 4
            [child_name] => "Pretty"
            [parent_name] => "Mr Thomas"
        )

    [4] => Array
        (
            [id] => 5
            [child_name] => "Raphel"
            [parent_name] => "Mr Brown"
        )

    [5] => Array
        (
            [id] => 6
            [child_name] => "Tommy"
            [parent_name] => "Mr Thomas"
        )

    [6] => Array
        (
            [id] => 7
            [child_name] => "Tim"
            [parent_name] => "Mr Thomas"
        )
)

From this array I want to generate a view like this :

enter image description here The parent_name field becomes the MainCategory and the child_name becomes the subcategory. The names have checkboxes in front of them.

How do I achieve this? I don't have much experience in php. I code in node js but this task needs this to be done in php. How do I do this?

Upvotes: 1

Views: 1197

Answers (2)

Nick
Nick

Reputation: 561

Try this, Here i use simple index() method and pass data to view file as an example.

You can use below code and test in your codeigniter.

Hope this will work for you.

Welcome.php (Controller)

public function index()
{
    $array = [
        [
            'id' => 1,
            'child_name' => "Emma",
            'parent_name' => "Mr Brown",
        ],
        [
            'id' => 2,
            'child_name' => "John",
            'parent_name' => "Mr Brown",
        ],
        [
            'id' => 3,
            'child_name' => "Joseph",
            'parent_name' => "Mr Thomas",
        ],
        [
            'id' => 4,
            'child_name' => "Pretty",
            'parent_name' => "Mr Thomas",
        ],
        [
            'id' => 5,
            'child_name' => "Raphel",
            'parent_name' => "Mr Brown",
        ],
        [
            'id' => 6,
            'child_name' => "Tommy",
            'parent_name' => "Mr Thomas",
        ],
        [
            'id' => 7,
            'child_name' => "Tim",
            'parent_name' => "Mr Thomas",
        ],
        [
            'id' => 8,
            'child_name' => "William",
            'parent_name' => "",
        ],
    ];

    $resultArray = [];
    foreach ($array as $key => $value) {
        if($value['parent_name'] != ''){
            $resultArray[$value['parent_name']][] = $value;
        }else{
            $resultArray[$value['child_name']] = [];
        }
    }
    $data = ['resultArray' => $resultArray];

    $this->load->view('welcome_message', $data);
}   

welcome_message.php (view)

<div>
    <form name='sample'>
        <?php 
        foreach ($resultArray as $parentKey => $parentValue) {
            echo '<input type="checkbox" name="'.$parentKey.'" value="'.$parentKey.'">'.$parentKey.'<br>';
            foreach ($parentValue as $childKey => $childValue) {
                echo '&nbsp;&nbsp;&nbsp;<input type="checkbox" name="'.$childValue['child_name'].'" value="'.$childValue['child_name'].'">'.$childValue['child_name'].'<br>';
            }
        }
        ?>
    </form>
</div>

Output

enter image description here

Upvotes: 1

Rahul
Rahul

Reputation: 18557

Check this,

Loop your array, it will capture all values with same parent_name.

$result = [];
foreach ($array as $key => $value) {
    $result[$value['parent_name']][] = $value['child_name']; // loop your array
}

It should work.

Upvotes: 1

Related Questions