使用 user_id codeigniter 获取用户信息
Posted
技术标签:
【中文标题】使用 user_id codeigniter 获取用户信息【英文标题】:Get user info with user_id codeigniter 【发布时间】:2018-03-07 08:36:44 【问题描述】:我正在 CodeIgniter 中创建一个网站,我想知道如何使用 user_id 行来回显一些用户数据并将其回显到个人资料视图页面上?
这是我的身份验证控制器(登录和注册)
<?php
public function login()
//laad login view
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
if ($this->form_validation->run() == TRUE)
$email = $_POST['email'];
$wachtwoord = ($_POST['wachtwoord']);
//check gebruiker in database
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('email' => $email, 'wachtwoord' => $wachtwoord));
$query = $this->db->get();
$user = $query->row();
//Als gebruiker bestaat
if ($user->email)
//tijdelijke berichten wanneer ingelogd of inloggen niet gelukt
$this->session->set_flashdata("success", "U bent nu ingelogd");
$_SESSION['user_logged'] = TRUE;
$_SESSION['user_id'] = $user->user_id;
$_SESSION['email'] = $user->email;
$_SESSION['voornaam'] = $user->voornaam;
$_SESSION['achternaam'] = $user->achternaam;
$_SESSION['woonplaats'] = $user->woonplaats;
$_SESSION['straat'] = $user->straat;
$_SESSION['huisnummer'] = $user->huisnummer;
$_SESSION['postcode'] = $user->postcode;
$_SESSION['beschrijving'] = $user->beschrijving;
$_SESSION['profiel_foto'] = $user->profiel_foto;
//link naar profiel pagina
redirect("user/profile", "refresh");
else
$this->session->set_flashdata('error', 'Invalid email or password');
//wanneer er een foutmelding is link weer naar de login pagina
redirect("https://kadokado-ferran10.c9users.io/auth/login", "refresh");
//laad login view
$this->load->view('login');
public function register()
if (isset($_POST['register']))
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
$this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
$this->form_validation->set_rules('wachtwoord', 'Herhaal wachtwoord', 'required|min_length[5]|matches[wachtwoord]');
$this->form_validation->set_rules('achternaam', 'Achternaam', 'required');
$this->form_validation->set_rules('postcode', 'Postcode', 'required');
$this->form_validation->set_rules('woonplaats', 'Woonplaats', 'required|min_length[3]');
$this->form_validation->set_rules('beschrijving', 'Beschrijving', 'required|min_length[5]');
$this->form_validation->set_rules('huisnummer', 'Huisnummer', 'required');
$this->form_validation->set_rules('geboortedatum', 'Geboortedatum', 'required');
$this->form_validation->set_rules('geslacht', 'Geslacht', 'required');
//If form validation true
if ($this->form_validation->run() == TRUE)
// echo 'form validated';
$target_dir = "upload/";
$target_file = $target_dir . time() . basename($_FILES["profiel_foto"]["name"]);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$imgName = time() . basename($_FILES["profiel_foto"]["name"]);
move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);
//voeg gebruiker toe aan database
$data = array(
'voornaam' => $_POST['voornaam'],
'achternaam' => $_POST['achternaam'],
'email' => $_POST['email'],
'wachtwoord' => ($_POST['wachtwoord']),
'startdatum' => date('Y-m-d'),
'postcode' => $_POST['postcode'],
'huisnummer' => $_POST['huisnummer'],
'woonplaats' => $_POST['woonplaats'],
'beschrijving' => $_POST['beschrijving'],
'geboortedatum' => $_POST['geboortedatum'],
'geslacht' => $_POST['geslacht'],
'profiel_foto' => $imgName
);
$this->db->insert('users', $data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
//Laad de registreer view
$this->load->view('register');
?>
假设我想在个人资料视图页面上回显 2 行电子邮件和 voornaam。我该怎么做?
【问题讨论】:
【参考方案1】:您可以在模型中粘贴代码
function get_user_by_id($id = null)
if($id!= null)
$condition = array('id' => $id);
$result = $this->main_model->get($condition, 'table-name');
foreach($result as $row)
return $row->fname.' '.$row->lname;
else
$data = $this->session->userdata('session-name');
$result = $this->main_model->get(array('id' => $data['id']), 'table-name');
foreach($result as $row)
return $row->fname.' '.$row->lname;
也可以像下面这样调用
echo $this->model_name->get_user_by_id(5);
如果您传递用户 ID,则返回用户名,否则返回会话用户名 您应该根据需要更改数据库字段名称
【讨论】:
【参考方案2】:您只需将数据传递到控制器中,然后您就可以使用视图中的所有数据来回显您想要的任何内容,$data 应该是数组或对象:
$this->load->view('register',$data);
在视图中你可以访问它。
<?php $voornaam ?>
<?php $email ?>
更多信息可以阅读官方文档: https://www.codeigniter.com/user_guide/general/views.html
【讨论】:
当我尝试加载我的视图配置文件时得到一个空白页 ini_set('display_errors', 1); ini_set('display_startup_errors', 1);错误报告(E_ALL);有关更多信息,请参阅此链接:***.com/questions/1053424/… 打开我上面提到的所有这些选项。 我需要把视图页面上的那些错误转过来对吗? 这里可以帮助找到php.ini文件:***.com/questions/6185319/…【参考方案3】:$this->load->view('register',$data);
将所有值作为数组放入控制器中。
在视图文件中。你可以像下面这样使用
<?=$voornaam ?>
<?=achternaam ?>
【讨论】:
我得到一个空白页你知道为什么吗? 请参考这里。 ***.com/questions/1053424/… 我在 php ini 文件中打开了 display_errors,但它仍然显示一个空白页?以上是关于使用 user_id codeigniter 获取用户信息的主要内容,如果未能解决你的问题,请参考以下文章