I might not even be thinking about this the correct way or asking it the correct way because I'm very new to CodeIgniter and my PHP skills are not too great yet. Basically, what I have is a page (view) that accepts values from a $content
array. That part works fine. I also have some data that I am pulling from a database that also needs to be displayed on the page. I am having trouble coming up with a way to get the values from $content
AND my values from the database on to my page.
So, long story short: How can I grab content from the database, set content values in my controller, and pass both to my view?
Here is my model code:
class Employees_model extends CI_Model {
function get_employees(){
$query = $this->db->get('employees');
return $query->result();
}
}
And here is my controller function:
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
}
}
Top of my head, you could do something like this:
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
}
}