Kevin Kaburu Kevin Kaburu - 1 month ago 15
PHP Question

Populate a dynamic Table from two arrays

The first array contains the headers and the other array contains the data to be populated. I am able to create the headers but I am having issues populating the data according to the headers.

$columns =([0]=>firstname, [1]=>lastname, [2]=>class);


And the data.

$data=(
array[0](['firstname']=>kevin, ['lastname']=>kaburu, ['class']=>1)
array[1](['firstname']=>kevin, ['lastname']=>kaburu, ['class']=>1)
array[2](['firstname']=>kevin, ['lastname']=>kaburu, ['class']=>1)
array[3](['firstname']=>kevin, ['lastname']=>kaburu, ['class']=>1))


Let me make this more clear

The thing is that I have two table the first one has columns while the other one has records, with foreign key pointing to the column in which they belong. The columns keep on changing so it is extremely dynamic. I want to populatw this data into a table ...
give me the available options.

Answer

At last I managed to sort the issue.... This is my code

Action.php

`

$recdata = $sql->showuploadedrecords($evntid);
        $coldata =$sql->showuploadedcols($evntid);
        $colmns = array();
        foreach ($coldata as $ky => $valu) {
            $colname = $valu['column_name'];
            $colmns[] = $colname;
        }


        $records = array();
        $total = count($recdata)/count($colmns);
        for ($i = 1; $i < $total +1; $i++) {
             $one =array();
            foreach ($recdata as $k => $v) {

                if($v['relate_records']==$i){
                   $one[]=$v;

                }

            }
             $records[] = $one;


        }



        $this->render('ViewUploaded', array('colmns' => $colmns,
            'data' => $records));
    }`

And on my View:

`echo '<tr role="row">';
            foreach ($colmns as $value) {
                echo '<th  class="sorting_asc" role="columnheader" tabindex="0" aria-controls="DataTables_Table_2" rowspan="1" colspan="1" style="width: 219px;" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending">'. $value . '</th>';   

            }
            echo '</tr>';
            ?>

                    </thead>



            <?php

                       foreach ($data as $key => $v) {
                            echo '<tr>'; 

                          for ($k = 0; $k < count($colmns); $k++) {

                       foreach ($v as $cell) {

                           if($cell['column_name']== $colmns[$k]){

                           echo'<td>'.$cell['item'].'</td>';

                           }
                       }

                       }

                        echo '</tr>'; 


                       }



          ?>







                        </tbody>

                 </table>`
Comments