Search code examples
phpdynamiccodeigniter-3

How to show db columns and data dynamically in codeigniter


I am getting table data from database and showing it in db but problem occurs. The quiz column in db is created dynamically when admin add quiz marks. Now when i am unable to show data of each column against quiz heading.

I am able to show quiz number in tag using foreach but i am unable to show marks against that quiz. Kindly guide me.

//Manage Grades_model.php

public function GetAllQuizMarks()
    {
    	$this->db->select('*');
        $this->db->from('quizmarks');
        
        $query = $this->db->get();
       	return $query->result();
    }

//ManageGrades.php

//this is controller

public function GetAllQuizMarks()
    {
        $data['markslist']=$this->ManageGrades_model->GetAllQuizMarks();
        
        $this->load->view('Manage/GetAllQuizMarks',$data);
    }

//This is view file

<div class="panel-body">
                            
                            <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                                <div class="row">
                                    <div class="col-sm-12">
                                        <table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer dtr-inline" id="dataTables-example" role="grid" aria-describedby="dataTables-example_info" style="width: 100%;">
                                <thead>
                                    <tr role="row">
                                        <th>Sr #</th>
                                        <th>Name</th>
                                        <th>Roll No.</th>
                                        
                                        
                                        <!-- <th>Actions</th> -->
                                        <?php 
                                    $Counter=1;
                                    
                                    ?>
                                        <?php 
                                        foreach($markslist as $designation)
                                        {
                                        ?><th><?php echo 'quiz'.$Counter;?></th>
                                     <?php 
                                        $Counter++;} 
                                        ?>
                                        
                                    </tr>
                                </thead>
                                <tbody>
                                 <?php 
                                
                                      $Counter1=1;
                                      
                                 foreach($markslist as $designation)
                                        {
                                      ?>   
                                    
                                <tr class="gradeA odd" role="row">
                                    <td class="sorting_1"><?php echo $Counter;?></td>
                                        
                                        <td class="sorting_1"><?php echo $designation->name;?></td>
                                        <td class="sorting_1"><?php echo $designation->rollno;?></td>
                                        
                                        
                                        <td class="sorting_1"><?php echo $designation->'quiz'.$Counter1;?></td>
                                        <!-- <td class="center">
                                            <div class="btn-group">
                                                    
                                                    <a href="<?php echo base_url()?>Mid/EditMid/<?php echo $designation->Id;?>"><button class="btn btn-warning">Update</button></a>
                                                    <a href="<?php echo base_url()?>Mid/DeleteMid/<?php echo $designation->Id;?>"><button class="btn btn-danger">Delete</button></a>
                                            </div>
                                        </td> -->
                                        
                                </tr>
                                      <?php
                                      $Counter1++;
                                    }
                                      ?>
                              </tbody>
                            </table></div></div></div>
                            <!-- /.table-responsive -->
                            
                        
                        </div>


Solution

  • Here this might help you.

    //This is view file
    
    <div class="panel-body">
    
    <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
        <div class="row">
            <div class="col-sm-12">
                <table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer dtr-inline" id="dataTables-example" role="grid" aria-describedby="dataTables-example_info" style="width: 100%;">
                    <thead>
                        <tr role="row">
                            <th>Sr #</th>
                            <th>Name</th>
                            <th>Roll No.</th>
                            <th>Quiz marks</th>
    
                        </tr>
                    </thead>
                    <tbody>
                        <?php
    
                        $Counter1=1;
    
                        foreach($markslist as $designation)
                        {
                        ?>
    
                        <tr class="gradeA odd" role="row">
                            <td class="sorting_1"><?php echo $Counter1++;?></td>
                            <td class="sorting_1"><?php echo $designation->name;?></td>
                            <td class="sorting_1"><?php echo $designation->rollno;?></td>
                            <td class="sorting_1"><?php echo $designation->marks; //use you column name for mark mere?></td>
                        </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>