PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Populate a dynamic Table from two arrays

 February 20, 2022     arrays, dynamic-tables, html, php, yii     No comments   

Issue

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.


Solution

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>`


Answered By - Kevin Kaburu
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing