CodeIgniter,将数据从模型传递到控制器以显示sql操作结果在控制器中

时间:2016-04-05 07:02:36

标签: php codeigniter

这是我的登录控制器

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

    class Login extends CI_Controller
    {

        function __construct()
        {
            parent::__construct();

        }

        public function index($msg = NULL)
        {
            $this->load->helper('url');
            // Load the model
            $this->load->model('login_model');
            // Validate the user can login
            $this->login_model->loginAction();      
        }   
    }
    ?>

我的模型 Login_model

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

class Login_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();

    }

    public function loginAction()
    {
        // grab user input
        $email = $this->input->get('email');
        $password = $this->input->get('password');

        // Prep the query
        $this->db->where('email', $email);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('users');

        foreach ($query->result() as $row)
        {
            $dbid= $row->id;
            $dbemail= $row->email;
        }

        $value=$query->num_rows();
        if($value == 1)
        {
            $details = array(
            'status'=>'sucess', 
            'message'=>'sucessfully logedin ',

            'id' => $dbid,
            'email' => $dbemail, 
            );
            echo  json_encode($details);
        }
        else
        {
            $detail = array(
            'status'=>'failed', 
            'message'=>'invalid email password ',

            );
            echo  json_encode($detail);
            return false;
        }
    }   
}
?>

这是有效的,但我需要使用 $ details &amp;我的登录控制器中的 $ detail 数组用于编码json数据。为此我需要在我的模型和控制器中进行所有修改。我是codeIgniter的新手。

1 个答案:

答案 0 :(得分:0)

进行如下更改

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

    class Login extends CI_Controller
    {

        function __construct()
        {
            parent::__construct();

        }

        public function index($msg = NULL)
        {
              $this->load->helper('url');
            // Load the model
            $this->load->model('login_model');
            // Validate the user can login
            $result = $this->login_model->loginAction();
            if($result['status'])
            {
               $details = array(
                           'status'=>'sucess', 
                           'message'=>'sucessfully logedin ',

                           'id' => $result['data']->id,
                           'email' => $result['data']->email, 
                          );
             }else{
                       $detail = array(
                             'status'=>'failed', 
                             'message'=>'invalid email password '
                     );

             }
             echo  json_encode($detail);
        }   
    }
    ?>

我的模型 Login_model

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

class Login_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();

    }

    public function loginAction()
    {
        // grab user input
        $email = $this->input->get('email');
        $password = $this->input->get('password');

        // Prep the query
        $this->db->where('email', $email);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('users');

        $value=$query->num_rows();
        if($value == 1)
        {
            return ['status'=>true, 'data'=>$query->row_object()];
        }
        else
        {
            return ['status'=>false];
        }
    }   
}
?>