• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

如何将变量从控制器传递到模型

php 来源:Amaan Iqbal 11次浏览

我只想将使用$ this-> input-> post() 获取输入的变量传递给Modal中的函数。如何将变量从控制器传递到模型

我的型号功能是

function get_name($email) { 
    //$email = $this->input->post('email'); 
    $this->db->select('first_name'); 
    $this->db->where('email', $email); 
    $query = $this->db->get('sign_up'); 
    return $query->row()->first_name; 
} 

作为回报我想FIRST_NAME从功能

===========解决方案如下:

First of all & All above.. User Input Must Be Pass Through Proper Validation & Sanitization

,您可以使用以下控制器&型号举例,让您的结果。

用户控制器:

<?php 
// No direct Script execution 
defined('BASEPATH') or exit('No direct script access allowed'); 
/** 
* My User Controller 
* 
* Always make Controller Slim & Model Fat :) 
*/ 
class User extends CI_Controller 
{ 
    /** 
    * Class Constructor 
    */ 
    public function __construct() 
    { 
     // call parent constructor 
     parent::__construct(); 
     // load your User_model 
     $this->load->model('User_model'); 
    } 

    /** 
    * If method calling by Via Via... 
    * @return [type] [description] 
    */ 
    public function validate_post(){ 
     // get email from POST 
     $email = $this->input->post('email', true); 
     // call another method 
     $this->get_name($email); 
    } 

    /** 
    * Get User First Name by Email ID 
    * @return [type] [description] 
    */ 
    public function get_name($email) 
    { 
     // if user has session email 
     if ($this->session->userdata('email')) {    
      // call model method to get first name 
      $first_name = $this->User_model->get_name($email); 

      // check what model returns 
      if ($first_name === false) { 
       // no record found 
       show_error('Data error: Supplied email not in record.'); 
      } elseif ($first_name === null) { 
       // invalid email format 
       show_error('Validation error: Email is not valid.'); 
      } else { 
       // Yes! you made it 
       $this->display('Welcome Back !!! '.$first_name); 
      } 
     } else { 
      // user not login, show them login page 
      redirect('/'); 
     } 
    } 
} 

/* End of file User.php */ 
/* Location: ./application/controllers/User.php */ 

User_model型号:

<?php 
// No direct script execution 
defined('BASEPATH') or exit('No direct script access allowed'); 
/** 
* Class User_model to handle all user related information from MySQL 
*/ 
class User_model extends CI_Model 
{ 
    /** 
    * MySQL table which contains all data about users 
    * @var string 
    */ 
    protected $table = 'sign_up'; 

    /** 
    * Returns, User First Name by Email ID 
    * @param [type] $email_addres [description] 
    * @return [type] [description] 
    */ 
    public function get_name($email_addres) 
    { 

     // validate email 
     if (filter_var($email_addres, FILTER_VALIDATE_EMAIL)) { 
      // desire column from table 
      $this->db->select('first_name'); 
      // where clause 
      $this->db->where('email_addres', $email_addres); 
      // mysql table 
      $query = $this->db->get($this->table); 

      // if record exist 
      if ($query->num_rows() > 0) { 
       // row, will return first result only, no matter if multiple record exist 
       $ret = $query->row(); 
       // return first row, firs_name value 
       return $ret->first_name; 
      } else { 
       // error 
       return false; 
      } 
     } else { 
      // validation error 
      return null; 
     } 
    } 
} 

/* End of file User_model.php */ 
/* Location: ./application/models/User_model.php */ 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)