0

我什至可能没有以正确的方式思考这个问题或以正确的方式提出这个问题,因为我对 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
    }


}
4

1 回答 1

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
            }


    }
于 2013-06-19T15:34:22.407 回答