0

Im trying to display each set of Array(first dimension) on a table. There will be a lot of array and table to be created in each page load, that's why I thought of using foreach. The array returned is from the controller.

My returned array looks like this:

Array: $appreq

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Name] => Lot Survey Plans and Specifications
                    [Document] => -
                    [Id] => 10
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Others (Specify)
                    [Document] => -
                    [Id] => 11
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Name] => Fire Protection Plan (if applicable)
                    [Document] => 
                    [Id] => 12
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Name] => Vicinity Map or Location Plan within a Two(2) Kilometer Radius
                    [Document] => -
                    [Id] => 13
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [1] => Array
                (
                    [Name] => Site Development Plan
                    [Document] => assets/uploads/ca060d9b-93d2-4fcd-8a8b-113bfac8dbbf/Architectural Permit/Site Development Plan...hollow-knight.jpg
                    [Id] => 14
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

            [2] => Array
                (
                    [Name] => Perspective
                    [Document] => -
                    [Id] => 15
                    [Is_Received] => 0
                    [Is_Personal_Submission] => 1
                )

Array: $ancillarypermit

enter image description here

The table should be displaying the data as divided in the first D array, then the rows is the next dimension of the array and my td should contain the: Name and Is_Received value. Please dont mind the table titles(Local Clearance etc), its part of another foreach loop, Im having trouble just with the tbody.

From the array: $appreq

the first dimensional: [0] => Array is the Local Clearance table

2 => Array is the Fire Safety table

3 => Array is the Architectural Permit table

EXPECTED TABLE:

table1

There must be something wrong with how I used my foreach variables, and how I call the array to show in the . I really hope someone can help me through this, thanks in advance

UPDATE --------------

I tried doing the following as suggested:

<?php foreach($ancillarypermit as $row => $data) : 
     foreach($appreq as $row => $value) : ?>

    <table style="text-align: left; width: 50%">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th width='10' style="text-align:center;">Received</th>
            <th scope="col">Requirement</th>
            </tr>
        </thead>
        <tbody>
        <h5><strong><?= $data->permit->Name ?></strong></h5>
        <?php 
            foreach ($value as $key => $values) :
            ?>
                <tr>
                    <th scope="row">1</th>
                    <td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
                        <?php 
                        echo CheckboxX::widget([
                            'name'=>'s_1', 
                            'value'=>$values['Is_Received'], 
                            // 'readonly'=>true, 
                            'pluginOptions'=>['threeState'=>false]
                        ]);
                        ?>
                    </td>
                    <td> <?= $values['Name']  ?> </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach; 
endforeach;?>

but im getting this result: enter image description here

2
  • can you please add expected html table to be displayed. Commented Aug 1, 2019 at 9:35
  • @InsaneSkull Hi, I updated my question and added an image, thanks Commented Aug 1, 2019 at 23:58

1 Answer 1

2

What you are doing is, you are looping the array twice, thus going 2 times in. On the first iteration $data would be

Array(
    [0] => Array(
        [Name] => Lot Survey Plans and Specifications
        [Document] => -
        [Id] => 10
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
    [1] => Array(
        [Name] => Others (Specify)
        [Document] => -
        [Id] => 11
        [Is_Received] => 0
        [Is_Personal_Submission] => 1
    )
)

On the first iteration of the 2nd foreach, $values would be

Array(
    [Name] => Lot Survey Plans and Specifications
    [Document] => -
    [Id] => 10
    [Is_Received] => 0
    [Is_Personal_Submission] => 1
)

UPDATE:

Your code should look something like

<?php 
    foreach($appreq as $row => $data): 
    // table name print
    // table headers print
        foreach ($data as $key => $values) :
            //print row content by calling $values['Is_Received'], $values['Name'], ...
        endforeach;
    endforeach
?>

I hope this answers your question. Please, let me know if there is anything else unclear.

UPDATE 2

<?php 
    foreach($appreq as $row => $value) : ?>

    <table style="text-align: left; width: 50%">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th width='10' style="text-align:center;">Received</th>
            <th scope="col">Requirement</th>
            </tr>
        </thead>
        <tbody>
        <h5><strong><?= $ancillarypermit[$row]->permit->Name ?></strong></h5>
        <?php 
            foreach ($value as $key => $values) :
            ?>
                <tr>
                    <th scope="row">1</th>
                    <td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
                        <?php 
                        echo CheckboxX::widget([
                            'name'=>'s_1', 
                            'value'=>$values['Is_Received'], 
                            // 'readonly'=>true, 
                            'pluginOptions'=>['threeState'=>false]
                        ]);
                        ?>
                    </td>
                    <td> <?= $values['Name']  ?> </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endforeach;?>
Sign up to request clarification or add additional context in comments.

11 Comments

You posted this answer faster, i deleted my one :)
@wolfQueen I hope this will answer your question. Please let me know if there is anything else. You can see below UPDATE
@wolfQueen. I cannot write you the code at this moment, but please not in the above example, you have things to put between the 2 for. Put the new tables headers there.
@wolfQueen can you show me the $ancillarypermi array? If you can edit the question to show the table array, I can tell you how to handle it.
@wolfQueen I have edited my answer. I am not quite sure if it will work though, as I am not 100% sure how does the structure look. If it doesn't, I suggest switch to chat, so we do not spam here and I will look into it and help you and later edit the answer with the correct final information.
|

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.