从代码点火器中的模型向控制器返回信息


Returning info to controller from model in codeigniter

我有这个模型函数:

        public function set_news()
        {
            $this->load->helper('url');
            $slug = url_title($this->input->post('title'), 'dash', TRUE);
            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $slug,
                'text' => $this->input->post('text')
            );
            return $this->db->insert('news', $data);
        }

如您所见,其中有一个名为$slug的变量。我调用这个函数的方式是通过一个控制器:

public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');
            $data['title'] = 'Create a news item';
            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');
            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');
            }
            else
            {
                $var = $this->news_model->set_news();
                $this->load->view('news/SLUG/');
            }
        }

现在,我想知道如何在控制器中使用$slug变量来加载刚刚发布的文章的view,但我不知道如何在数据提交到数据库后访问该变量。

您可以尝试使用此

控制器

public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');
            $data['title'] = 'Create a news item';
            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');
            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');
            }
            else
            {
                $var['r'] = $this->news_model->set_news();
                $this->load->view('news/SLUG', $var);
            }
        }

型号news_model.php

public function set_news()
        {
            $this->load->helper('url');
            $slug = url_title($this->input->post('title'), 'dash', TRUE);
            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $slug,
                'text' => $this->input->post('text')
            );
            $this->db->insert('news', $data);
            $id = $this->db->insert_id();
           return $this->db->get_where('news', array('id' => $id))->result();
        }

查看新闻/SLUG.php

<pre>
    <?php var_dump($r);?>
</pre>