0

I'm making a website in the CodeIgniter and I'm wondering how I can echo some user data with using the user_id row and echo it on the profile view page?

This is my auth controller (login and registration)

<?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');
}
?>

So lets say I want to echo the 2 rows email and voornaam on a profile view page. How can I do that?

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
lablanco
  • 103
  • 1
  • 13

3 Answers3

1

you just have to pass your data into your controller, and then you can use all data to your view to echo whatever you want, $data should be an array or an object:

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

In view you can access it.

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

For further Information you can read official documentation: https://www.codeigniter.com/user_guide/general/views.html

Ahmad Hassan
  • 371
  • 4
  • 22
0
$this->load->view('register',$data);

Put all the values as an array in your controller.

In the view file. you can use like bellow

<?=$voornaam ?>
<?=achternaam ?>
Bira
  • 4,531
  • 2
  • 27
  • 42
0

You can paste code in your model

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;
    }
    }
}

and also call like below

echo $this->model_name->get_user_by_id(5);

If you pass user id then it return username otherwise it return session username and you should change database field name as per your need

Suresh Suthar
  • 794
  • 8
  • 15