我只想传递一个变量,其输入是使用$ 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答案 0 :(得分:1)
请检查下面提到的解决方案。
function get_name($email) {
$this->db->select('first_name');
$this->db->where('email', $email);
$query = $this->db->get('sign_up');
$ret = $query->row();
return $ret->first_name;
}
如果不起作用,请告诉我。
答案 1 :(得分:1)
首先&以上所有..用户输入必须通过适当的验证&消毒 强>
现在,您可以使用以下Controller&获得结果的模型示例。
用户控制器:
<?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 */
答案 2 :(得分:0)
如果我理解正确,你想找到&#34; first_name&#34;在表格&#34; sign_up&#34;,其中&#34;电子邮件&#34; = $ email。你尝试过这种方法吗?
$this->db->select("first_name");
$this->db->from("sign_up");
$this->db->where("email", $email);
$result = $this->db->get()->row();
答案 3 :(得分:0)
你应该将变量从Model传递给Controller,而不是从Controller传递给Model(对于这个例子。因为你从数据库中获取数据,你应该使用Models来完成它)。我认为你应该在开始编码之前理解MVC逻辑。
型号:
public function get_name()
{
$email = $this->input->post('email', true);
$query = $this->db->get_where('sign_up', ['email' => $email])->row('first_name');
return $query;
}
控制器:
public function some_function_name()
{
$this->load->model('Your_model_name');
$some_variable = $this->Your_model_name->get_name();
}
注意:如果您不使用PHP7,请使用array()
代替括号。
答案 4 :(得分:0)
请尝试以下代码获取名字:
function get_name($email) {
$firstName = "";
$this->db->select("first_name");
$this->db->from("sign_up");
$this->db->where("email", $email);
$query = $this->db->get();
$result = $query->result_array();
if(!empty($result)){
$firstName = $result[0]['first_name'];
}
return $firstName;
}
在上面的函数中,如果存在第一个名称,那么它将被获取并以字符串格式发送给控制器。
请注意,如果有很多行,那么它也会从结果的第一行获取数据,因为我已经把它作为第0个元素。