dragon8837 2011-06-03 14:37
浏览 18

如何使用模型检索数据并将其传递给使用CodeIgniter的控制器?

From a user login form i need to use their username ($this->input->post('username')) to query against the membership table in my database so that I can retrieve the users firstname and lastname, which I believe should be done via a model called from my controller. I then need to pass that data back to my controller to use the firstname and lastname to add in to my session userdata which is set in my controller, as shown below.

$data = array(
   'username' => $this->input->post('username'),
   'firstname' => //need to retrieve value from database,
   'lastname' => //need to retrieve value from database,
   'is_logged_in' => true
);

$this->session->set_userdata($data);

Thanks, Kristan.

  • 写回答

3条回答 默认 最新

  • dongyi5425 2011-06-03 14:45
    关注

    Please check CodeIgniter user guide for model. The example of Blog controller and Blog model will answer you.

    class Blogmodel extends CI_Model {
    
        var $title   = '';
        var $content = '';
        var $date    = '';
    
        function __construct()
        {
            // Call the Model constructor
            parent::__construct();
        }
    
        function get_last_ten_entries()
        {
            $query = $this->db->get('entries', 10);
            return $query->result();
        }
    
        function insert_entry()
        {
            $this->title   = $_POST['title']; // please read the below note
            $this->content = $_POST['content'];
            $this->date    = time();
    
            $this->db->insert('entries', $this);
        }
    
        function update_entry()
        {
            $this->title   = $_POST['title'];
            $this->content = $_POST['content'];
            $this->date    = time();
    
            $this->db->update('entries', $this, array('id' => $_POST['id']));
        }
    
    }
    
    class Blog_controller extends CI_Controller {
    
        function blog()
        {
            $this->load->model('Blog');
    
            $data['query'] = $this->Blog->get_last_ten_entries();
    
            $this->load->view('blog', $data);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答