简体   繁体   English

如何获得模型值给控制器?

[英]How to get model value to controller?

I am using codeigniter 3.1 . 我正在使用codeigniter 3.1。

I want to pass posted data through model, but to pass login i need to get model value to controller ? 我想通过模型传递发布的数据,但是要通过登录我需要将模型值传递给控制器​​?

How to get model value to controller? 如何获得模型值给控制器?

Controller 调节器

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller 
{
    public function __construct() 
    {
         parent::__construct();
         $this->load->model("user_login");
    }

    public function index()
    {

        $post = $this->input->post("post", true);

        if ($this->user_login->check($post)) {
            if ($this->user_login->logins == "pass") {  
                echo "valid";
            } else {
                echo "Invalid";
            }
        }
    }
} 
?>

Model 模型

<?php

class User_Login extends CI_Model 
{
    var $logins=false;

    public function check($post) 
    {
        $results = $this->db->where('username', $post)
                            ->get('users');

        if($results->num_rows() > 0){
            $this->logins = "pass";
        } else {
            $this->logins = "fail";
        }
    }
}
?>

Controller 控制者

class Login extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model("user_login");
    }

    public function index()
    {

       $post = $this->input->post("post", true);

       if ($this->user_login->check($post)==true) {
          echo "valid";
       }
       else{
          echo "invalid";
       }
   }
} 
?>

modal 情态的

 <?php

 class User_Login extends CI_Model 
 {
    var $logins=false;

    public function check($post) 
    {
       $results = $this->db->where('username', $post)
                        ->get('users');

       if($results->num_rows() > 0){
          return true;
      } else {
          return false;
      }
   }
 }
?>

1) At first, if you want to use any public variable, no need to use var with it. 1)首先,如果要使用任何公共变量,则无需将其与var一起使用。 Just write $logins=false; 只需写$logins=false;

2) Next you should something from your model for checking this type of thing. 2)接下来,您应该从模型中检查某些东西以检查这种类型的东西。 Like return "pass" or return "fail" return "pass"return "fail"

3) After that when you check it from controller, you can keep this return data to a variable like this $return_value = $this->user_login->check($post) . 3)之后,当您从控制器检查它时,可以将此返回数据保留到类似$return_value = $this->user_login->check($post)的变量中。 At last, it can be like below code - 最后,就像下面的代码一样-

 if ($return_value) {
    if ($return_value == "pass") {   
    echo "valid";
   } else {
    echo "Invalid";
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM