使用模型视图和控制器在codeignator中显示数据的过程是什么?
控制器
public function SecondarySchoolLevel() {
$this->load->model('Schoolprogram_model');
$data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);
$this->load->view('SchoolPrograms', $data);
}
查看
$query = $this->db->query("SELECT * from programs where progcategoryid = $progcategoryid");
foreach ($query->result_array() as $row){
?>
<tr id="r<?php echo $row["progid"]; ?>">
<td><?php echo $row["progcode"]; ?></td>
<td><?php echo $row["activities"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td><?php echo $row["session"]; ?></td>
<td><?php echo $row["hours"]; ?></td>
<td><?php echo $row["cost"]; ?></td>
<td><?php echo $row["progcategoryid"]; ?></td>
<td><?php echo $row["agefrom"]; ?></td>
<td><?php echo $row["ageto"]; ?></td>
模型
function getschoolprog($progcategoryid) {
$this->db->where('progcategoryid', $progcategoryid);
$query = $this->db->get('programs');
return $query->result();
}
这是我的代码。
答案 0 :(得分:3)
<强>控制器强>
public function SecondarySchoolLevel() {
$this->load->model('Schoolprogram_model');
$data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);//result of your query is stored in this ($data['progcategoryid']) variable
$this->load->view('SchoolPrograms', $data);
}
<强>模型强>
function getschoolprog($progcategoryid) {
$this->db->select('*');
$this->db->where('progcategoryid', $progcategoryid);
$query = $this->db->get('programs');
return $query->result_array();//<---- returns you an array
}
查看强>
foreach ($progcategoryid as $key => $row) { ?>
<tr id='r<?php echo $row["progid"]; ?>'>
<td><?php echo $row["progcode"]; ?></td>
<td><?php echo $row["activities"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td><?php echo $row["session"]; ?></td>
<td><?php echo $row["hours"]; ?></td>
<td><?php echo $row["cost"]; ?></td>
<td><?php echo $row["progcategoryid"]; ?></td>
<td><?php echo $row["agefrom"]; ?></td>
<td><?php echo $row["ageto"]; ?></td>
</tr>
<?php } ?>
答案 1 :(得分:1)
<强>控制器强>
$data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);
$this->load->view('SchoolPrograms', $data);
查看强>
<?php foreach($progcategoryid as $row)
{
?>
<td><?php echo $row->progcode; ?></td>// you can render your data like this
<?php }?>
无需在
中view
中编写查询,因为您已经编写过了 在model
。
您只需将数据发送至views