1

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?

0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

if one of the objects is [ 'id' => 1, 'child_name' => "Emma", 'parent_name' => "", ] i.e without the parent_name then in this case the child_name should become the parent name when displaying in the view. how to do it?
@node_man Please check my updated above code, here i take "id = 8" as your example and change my loop according to your requirement (please check output).
1

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.

Comments

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.