首页 文章

使用user_id codeigniter获取用户信息

提问于
浏览
0

我在CodeIgniter中创建一个网站,我想知道如何使用user_id行回显一些用户数据并在配置文件视图页面上回显它?

这是我的auth控制器(登录和注册)

<?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 . 我怎样才能做到这一点?

3 回答

  • 1

    您只需将数据传递到控制器中,然后就可以使用视图中的所有数据来回显您想要的任何内容,$ data应该是数组或对象:

    $this->load->view('register',$data);
    

    在视图中,您可以访问它 .

    <?php $voornaam ?>
    <?php $email ?>
    

    有关详细信息,请阅读官方文档:https://www.codeigniter.com/user_guide/general/views.html

  • 0
    $this->load->view('register',$data);
    

    将所有值作为数组放入控制器中 .

    在视图文件中 . 你可以像下面一样使用

    <?=$voornaam ?>
    <?=achternaam ?>
    
  • 0

    您可以在模型中粘贴代码

    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,则返回用户名,否则返回会话用户名,您应根据需要更改数据库字段名称

相关问题