Php 如何将数据从模型传递到控制器(CodeIgniter,会话帮助)
首先对不起我的英语(它不是我的主要语言) 我是CodeIgniter3的新手,我喜欢它 假设这是我的模型:Php 如何将数据从模型传递到控制器(CodeIgniter,会话帮助),php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,首先对不起我的英语(它不是我的主要语言) 我是CodeIgniter3的新手,我喜欢它 假设这是我的模型: function login($uname, $upassword) { $this->db->where('uname', $uname); $this->db->where('upassword', $upassword); $query = $this->db->get('zamestnanc
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
foreach ($query->result() as $row) {
$data['zamestnanec'] = $row->tpred_zamestnanci." ".$row->meno_zamestnanci. " ".$row->priezvisko_zamestnanci." ".$row->tza_zamestnanci;;
}
return ($data);
}
这是我的控制器:
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno);
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
我想问您如何将数据从模型传递/显示到会话。到$this->session->userdata('user')
您能给我解释一下将数据从模型传递到控制器以及从模型传递到会话的正确过程吗。(就像你试图向一个思考缓慢的人解释)
我不需要文档的链接,只需要一个或几个人可以在示例中进行解释。只要将模型传递给控制器,不管数据是否正确。模型中不需要一个大循环 在型号中
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
$result = $query->result_array();
$count = count($result); # get how many data passed
if ($count == 1) {
return $result;
}
else
{
return false;
}
}
function login($uname, $upassword)
{
$this->db->select("*");
$tthis->db->from("table_name")
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
// you can user result_array() to get all information in array form.
$this->result = $query->result_array();
return $this->result;
}
在控制器中
function loginUser()
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
//$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
$validate = $this->user_model->login($uname, $upassword);
if ($validate != false)
{
# Valid user
# $validate conating user details too. to check add this next line print_r($validate);die;
$this->session->set_userdata('user', $uname);
redirect('/otk/');
}
else
{
# Invalid User
redirect('/user/');
}
}
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno); // here you are setting up the session.
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
无论数据是否正确,只要将模型传递给控制器即可。模型中不需要一个大循环 在型号中
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
$result = $query->result_array();
$count = count($result); # get how many data passed
if ($count == 1) {
return $result;
}
else
{
return false;
}
}
function login($uname, $upassword)
{
$this->db->select("*");
$tthis->db->from("table_name")
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
// you can user result_array() to get all information in array form.
$this->result = $query->result_array();
return $this->result;
}
在控制器中
function loginUser()
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
//$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
$validate = $this->user_model->login($uname, $upassword);
if ($validate != false)
{
# Valid user
# $validate conating user details too. to check add this next line print_r($validate);die;
$this->session->set_userdata('user', $uname);
redirect('/otk/');
}
else
{
# Invalid User
redirect('/user/');
}
}
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno); // here you are setting up the session.
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
您可以通过两种方式将信息从模型传递到控制器
- 通过使用会话
- 首先使用查询获取信息并将该数组返回给控制器
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
$result = $query->result_array();
$count = count($result); # get how many data passed
if ($count == 1) {
return $result;
}
else
{
return false;
}
}
function login($uname, $upassword)
{
$this->db->select("*");
$tthis->db->from("table_name")
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
// you can user result_array() to get all information in array form.
$this->result = $query->result_array();
return $this->result;
}
在控制器中
function loginUser()
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
//$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
$validate = $this->user_model->login($uname, $upassword);
if ($validate != false)
{
# Valid user
# $validate conating user details too. to check add this next line print_r($validate);die;
$this->session->set_userdata('user', $uname);
redirect('/otk/');
}
else
{
# Invalid User
redirect('/user/');
}
}
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno); // here you are setting up the session.
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
您可以通过两种方式将信息从模型传递到控制器
- 通过使用会话
- 首先使用查询获取信息并将该数组返回给控制器
function login($uname, $upassword)
{
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
$result = $query->result_array();
$count = count($result); # get how many data passed
if ($count == 1) {
return $result;
}
else
{
return false;
}
}
function login($uname, $upassword)
{
$this->db->select("*");
$tthis->db->from("table_name")
$this->db->where('uname', $uname);
$this->db->where('upassword', $upassword);
$query = $this->db->get('zamestnanci');
// you can user result_array() to get all information in array form.
$this->result = $query->result_array();
return $this->result;
}
在控制器中
function loginUser()
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
//$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
$validate = $this->user_model->login($uname, $upassword);
if ($validate != false)
{
# Valid user
# $validate conating user details too. to check add this next line print_r($validate);die;
$this->session->set_userdata('user', $uname);
redirect('/otk/');
}
else
{
# Invalid User
redirect('/user/');
}
}
//Funkcia na prihlásenie používatela
function loginUser()
{
//Načítať model
$this->load->model('user_model');
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$meno = $this->user_model->login($uname, $upassword);
//Ak sa meno a heslo zhoduje
if ($this->user_model->login($uname, $upassword))
{
$this->session->set_userdata('user', $meno); // here you are setting up the session.
$data['user'] = $this->session->userdata('user');
redirect('/otk/', $data);
}
else
{
redirect('/user/');
}
}
希望这对您有所帮助:
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$lname = $this->input->post('lname');//example
$session_arr['uname'] = $uname;
$session_arr['fullname'] = $fname.' '.$lname; // example
$this->session->set_userdata($session_arr);
从模型中获取您想要的所有用户信息(在数组中):
在控制器中:
第一种方式:
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$lname = $this->input->post('lname');//example
$session_arr['uname'] = $uname;
$session_arr['fullname'] = $fname.' '.$lname; // example
$this->session->set_userdata($session_arr);
第二种方式:
$user = $this->user_model->login($uname, $upassword);
if ($user != false)
{
// Valid user
// $validate containing user details too. to check add this next line
// print_r($validate);die;
$this->session->set_userdata($user);
redirect('/otk/');
}
更多信息:
希望这对您有所帮助:
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$lname = $this->input->post('lname');//example
$session_arr['uname'] = $uname;
$session_arr['fullname'] = $fname.' '.$lname; // example
$this->session->set_userdata($session_arr);
从模型中获取您想要的所有用户信息(在数组中):
在控制器中:
第一种方式:
$uname = $this->input->post('uname');
$upassword = $this->input->post('upassword');
$lname = $this->input->post('lname');//example
$session_arr['uname'] = $uname;
$session_arr['fullname'] = $fname.' '.$lname; // example
$this->session->set_userdata($session_arr);
第二种方式:
$user = $this->user_model->login($uname, $upassword);
if ($user != false)
{
// Valid user
// $validate containing user details too. to check add this next line
// print_r($validate);die;
$this->session->set_userdata($user);
redirect('/otk/');
}
更多信息:
我还有一个问题,是否可以将函数login()中的所有数据传递给显示当前登录的用户。我的意思是显示他的名字、姓氏等,因为当我这样做时:$this->session->userdata('user'),它将显示:Logged as(我的登录用户名),我想显示表中的全名+姓氏。是的,您可以。添加此
echo$validate[0]['name_field'];死亡代码>内部if条件好,但现在当我尝试$this->session->set_userdata('user',$validate)时代码>和在我看来(“基本视图”);我做$this->session->userdata('user')代码>它将显示错误数组到字符串。但是当我想知道sessionprint\r($this->session->all\u userdata())中的所有用户数据时
它将显示正确的数据。@Nezo no手动分配它们,$this->session->set_userdata('userName',$validate[0]['name'])
和$this->session->set_userdata('birth',$validate[0]['bday\u field'])
。。LikeWiseOK谢谢你这项工作很完美,但是是否可以将此$this->session->set_userdata('userName',$validate[0]['name'])
$this->session->set_userdata('birth',$validate[0]['bday\u field'])
添加到例如数组$user,而不是视图中的$this->session userdata('user)代码>我还有一个问题,是否可以将函数login()中的所有数据传递给显示当前登录的用户。我的意思是显示他的名字、姓氏等,因为当我这样做时:$this->session->userdata('user'),它将显示:Logged as(我的登录用户名),我想显示表中的全名+姓氏。是的,您可以。添加此echo$validate[0]['name_field'];死亡代码>内部if条件好,但现在当我尝试$this->session->set_userdata('user',$validate)时代码>和在我看来(“基本视图”);我做$this->session->userdata('user')代码>它将显示错误数组到字符串。但是当我想知道sessionprint\r($this->session->all\u userdata())中的所有用户数据时
它将显示正确的数据。@Nezo no手动分配它们,$this->session->set_userdata('userName',$validate[0]['name'])
和$this->session->set_userdata('birth',$validate[0]['bday\u field'])
。。LikeWiseOK谢谢你这项工作很完美,但是是否可以将此$this->session->set_userdata('userName',$validate[0]['name'])
$this->session->set_userdata('birth',$validate[0]['bday\u field'])
添加到例如数组$user,而不是视图中的$this->session userdata('user)代码>