在codeigniter中的1个表中分隔具有相同标题的行

时间:2017-09-23 10:00:32

标签: php html codeigniter codeigniter-2

我想分开我的"标题"具有相同的名称,并使用codeigniter将它们分组在单独的表中。这是我的表,期望的输出和我在下面的codeigniter中的MVC:

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      |

我的模特

public function view_evaluation(){      

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

}

我的控制器

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);
}

我的观点

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

我的学校项目请帮助我......提前感谢你..

2 个答案:

答案 0 :(得分:0)

试试这个例子,这样可以正常工作......

public function results (){

    $query   = $this->db->group_by('title');
    $query   = $this->db->get('evaluation');
    $results = $query->result();

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

public function singleres($title){
    $this->db->where('title', $title);
    $query = $this->db->get('evaluation');
    return $query->result();
}

答案 1 :(得分:0)

将值发送到视图

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

然后测试模型

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();
}}

然后创建视图文件

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