Question

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.

Was it helpful?

Solution 3

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

OTHER TIPS

insert this after your data declaration:

$dataProvider = new CArrayDataProvider($data, array(
    //CDataProvider options, sort, etc..
));

change the render call to include passing the dataProvider to your view:

$this->render('view', array(
    'dataProvider' => $dataProvider,
));

in view:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    // other CGridView options
)); ?>

Refer this Link:

http://www.yiiplayground.com/index.php?r=UiModule/dataview/gridViewArray

You can create gridview with use of CArrayDataProvider.

This link cotain example code which is exactly match with your requirement.

Without using Yii method:

<table>
<thead>
<tr>
<?php
  foreach($columns  as $s)
  {
     echo "<th>$s</th>";
   }
?>
</tr>
<tbody>
<?php
  foreach($data as $d)
  {
     echo "<tr>";
     foreach($d as $cols)
       echo "<td>".$cols."</td>";// hope all columns are present in second array.
     echo "</tr>";
  }
?>
</tbody>
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top