我可能甚至没有考虑这个正确的方法或者正确的方式,因为我对CodeIgniter很新,而且我的PHP技能还不是很好。基本上,我所拥有的是一个接受来自$content
数组的值的页面(视图)。那部分工作正常。我也有一些数据,我从数据库中提取,也需要在页面上显示。我无法想出一种方法来获取$content
和我的值从数据库到我的页面的值。
所以,长话短说:如何从数据库中获取内容,在控制器中设置内容值,并将两者都传递给我的视图?
这是我的型号代码:
class Employees_model extends CI_Model {
function get_employees(){
$query = $this->db->get('employees');
return $query->result();
}
}
这是我的控制器功能:
function create(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//getting employees content from the db
$this->load->model('Employees_model');
if($query = $this->Employees_model->get_employees()){
$content['employees'] = $query;
}
print_r($content); //this is displaying the correct content from the DB, but having trouble passing it to the view
$this->form_validation->set_rules('kpa1', 'KPA 1', 'trim|required');
$this->form_validation->set_rules('kpa1_rating', 'KPA 1 Rating', 'trim|required|greater_than[0]');
if ($this->form_validation->run() == FALSE){
$this->load->view('create_review', $content);
} else {
$data = array(
'kpa1' => $this->input->post('kpa1'),
'kpa1_rating' => $this->input->post('kpa1_rating')
);
$content = array(
'alert_type' => 'alert-success',
'message' => 'The review has been saved!'
);
$this->load->model('Review_model');
$this->Review_model->add_review($data);
$this->load->view('create_review', $content);
//this is only loading data from the $content array above
}
}
答案 0 :(得分:0)
我可以做到这一点:
function create(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$content = array();
//getting employees content from the db
$this->load->model('Employees_model');
if($query = $this->Employees_model->get_employees()){
$content['employees'] = $query;
}
print_r($content); //this is displaying the correct content from the DB, but having trouble passing it to the view
$this->form_validation->set_rules('kpa1', 'KPA 1', 'trim|required');
$this->form_validation->set_rules('kpa1_rating', 'KPA 1 Rating', 'trim|required|greater_than[0]');
if ($this->form_validation->run() == FALSE){
$this->load->view('create_review', $content);
} else {
$data = array(
'kpa1' => $this->input->post('kpa1'),
'kpa1_rating' => $this->input->post('kpa1_rating')
);
//pass data to model
$this->load->model('Review_model');
$this->Review_model->add_review($data);
//add more data
$data['kpa1'] = $this->input->post('kpa1');
$data['kpa1_rating'] = $this->input->post('kpa1_rating');
//see if we have employees data and if so add
if(isset($content['employees'])){
$data['employees'] = $content['employees'];
}
//pass data to view
$this->load->view('create_review', $data);
//this is only loading data from the $content array above
}
}