在codeigniter中从模型返回信息到控制器

时间:2016-05-06 18:06:00

标签: php 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 - 但我可以& #39;弄清楚在将数据提交到数据库后如何访问该变量。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

控制器

SignInManager

model news_model.php

var fs=require('fs');
var readable = fs.createReadStream("data.txt", {
  encoding: 'utf8',
  fd: null
});
var lines=[];//this is array not a string!!!
readable.on('readable', function() {
  var chunk,tmp='';
  while (null !== (chunk = readable.read(1))) {
    if(chunk==='\n'){
      tmp='';
      lines.push(tmp);
    //  here tmp is containing a line you can process it right here.
    //  As i am just pushing the line in the array lines.
    //  use async if you are processing it asynchronously.
    }else
      tmp+=chunk;
  }
});
readable.on('end',function(){
  var i=0,len=lines.length;
  while(i<len)
    console.log(lines[i++]);
});

查看新闻/ SLUG.php

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);
            }
        }