【问题标题】:Separate rows with same title in 1 table in codeigniter在codeigniter的1个表中分隔具有相同标题的行
【发布时间】:2017-09-23 10:00:31
【问题描述】:

我想将具有相同名称的“标题”分开,并使用 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>

我的学校项目请帮助我..提前非常感谢您..

【问题讨论】:

  • 欢迎来到 Stack Overflow。你已经尝试过什么来解决这个问题? 请求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的总结,以及您在解决问题时遇到的困难的描述。请查看How do I ask a good question。跨度>

标签: php html codeigniter codeigniter-2


【解决方案1】:

发送值以查看

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> 

【讨论】:

    【解决方案2】:

    试试这个例子,这会很好......

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

    【讨论】:

    • 是的@MarkJosephPino
    • 检查下面的例子@MarkJosephPino
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2015-10-25
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多