Search code examples
phphtmlcodeignitercodeigniter-2

Separate rows with same title in 1 table in codeigniter


I want to separate my "title" having same name and group them in separate table using codeigniter. Here is my Table, desired output and my MVC in codeigniter below:

my table: evaluation
|   id  |   instructor|   title       |   result  |
|   1   |   Mark      |   Module1     |   5       |
|   2   |   Dave      |   Module1     |   10      |
|   3   |   Noel      |   Module2     |   6       |
|   3   |   Mike      |   Module3     |   11      |

Desired output:
in table (Module1)
|   instructor|   result  |
|   Mark      |   5       |
|   Dave      |   10      |

in table (Module2)
|   instructor|   result  |
|   Noel      |   6       |

in table (Module3)
|   instructor|   result  |
|   Mike      |   11      |

My Model

public function view_evaluation(){      

    $this->db->select('*');
    $this->db->order_by('title');
    $this->db->from('evaluation');
     $query = $this->db->get()->result_array();
        return $query;

}

My controller

public function admin_view_evaluation()
{ 
   $view_evaluation = $this->admin_model->view_evaluation();
   $data = array('view_evaluation' => $view_evaluation);
   $this->load->view('admin/admin_view_evaluation_page', $data);
}

My View

<?php foreach($view_evaluation as $view){  ?>
 <table id="source" class="table table-bordered table-hover">
   <caption style="color: #fff;">
     <?php echo $view['title']; ?>
   </caption>
  <thead>
    <tr>
      <th>Instructor</th>
      <th>Results</th>
    </tr>
  </thead>
  <tbody>

   <?php foreach($view_evaluation as $get){  ?>
      <tr>
            <th><?php echo $get['instructor']; ?></th>
            <td><?php echo $get['result']; ?></td>
      </tr>
   <?php }  ?>
  </tbody>
</table>

My School Project Please Help me..Thank you so much in advance..


Solution

  • Sending the values to view

    public function results (){
        $this->load->model('Test_model');
        $this->load->view('testview');
    }
    

    Then Test Model

    Class Test_model extends CI_Model{
    public function onlytitlrresults(){
        $query   = $this->db->group_by('title');
        $query   = $this->db->get('evaluation');
        $results = $query->result();
        return $results;
    }
    public function singleres($title){
        $this->db->where('title', $title);
        $query = $this->db->get('evaluation');
        return $query->result();
    }}
    

    Then Create View File

    <ul>
    <?php $results = $this->Test_model->onlytitlrresults();
    $rrr = $this->Test_model->singleres($res->title);?>
    <li>
    <?php echo $res->title."<br/>"; ?>
    </li>
    <ul>
        <?php foreach($rrr as $r){?>
        <li><?php echo "Name  :".$r->instructor;?></li>
        </li><?php echo "Score :".$r->result;?></li>
        <?php }?>
    </ul>
    <?php }?>
    </ul>