Welcome Guest, Not a member yet? Register   Sign In
variable in controller for view

hello , how do I store the result of a field of a table in a variable in the controller and access that variable in the view ? what I wanted was to display the user's name in superios bar of my system , example after the user logs appears "Hello user"

thanks You

Ps . Excuse my English , I am Brazilian .

(This post was last modified: 04-06-2015, 10:01 AM by casa.)

Hello, i hope to answer your problem
PHP Code:
class User_controller extends CI_controler
$var 'toto' ;
$view_perso 'path_to_yourèview' // Example : 'page' located in 'views/page.php'

public function __construct()
parent::_cconstruct() ;
$this->load->model('User_model') ;
     *   here a function with a parameter but you can do it with session variables, cookies, etc.. choose the best thing for security and here, is not the best :( . I prefer sesssion variables 
public function get_first_view($id_user)
// since your model, call function which return information about the user like this for example
$user $this->User_model->getUser($id_user) ;  // return an object User by id_user. Load model User before do this, if its not loaded
$data = array('var_name' => $this->var,    // your datas to pass to the view
'var_name2' => 'my name is',
'name_user' => $user->getName())  ; // where getName() is the method who returns the name of your user object
         // load your view with datas
$this->load->view($this->view_perso$data) ;

//_____ in your view name page.php
echo 'Hello '.$var_name2.' '.$name_user.' and not '.$var_name // Hello  my name is Casa (if $user->getName() == 'Casa') and not toto 

have a good day

thanks casa,

as you would with session ? would you help me

(This post was last modified: 04-07-2015, 07:30 AM by casa.)

With Session, for example, at the connexion, you store a value in a session variable like this for example :
PHP Code:
In your controller, you send ajax request to avoid to pass variables by url and you send them by POST method.
You control that the session user is again available by controlling the existence of your session variable ($this->session->userdata('key')) defined before.
After you can do everything with session variables.
If you get a variable in your controller like the example before "public function get_first_view($id_user)", think to control the type of the variables. For example, here, if $id_user would be an integer, think to protect and cast the variable like this for example :
$id_user = (int) $this->security->xss_clean($id_user) ; // cast to integer and secure agaist xs attack
I hope that help you.
Refer to userguide of CI3 or CI2 switch your version.
Have a good day.

(This post was last modified: 04-09-2015, 09:14 AM by pompeu.)

Hello Casa ,

I looked and studied the documentation and I could do what I wanted , I used session , follows the code :

$query = $this->db->query("select email, usuario, senha from usuarios where email = '$email' and senha = '$senha'");
        foreach ($query->result() as $row)
                $usuario = $row->usuario;
        if ($query->num_rows() > 0) {

            //if username and password are correct the session begins and will be redirected to the main screen
            $add_session = array('logado' => '1', 'email' => $email, 'nome_usuario' => $usuario, 'senha' => $senha);

        }    else    {

            //    If the password / user are incorrect, then send the user back to the login screen with an error
            $this->session->set_userdata('dados_incorretos', 'dados_incorretos');


Thank you for your help , thanks very much

Problem resolved ^^

(This post was last modified: 04-09-2015, 09:15 PM by casa.)

It's with pleasure. Smile

I advise you that for your query (to secure it)
PHP Code:
$query $this->db->query("select email, usuario, senha from usuarios where email = '$email' and senha = '$senha'");

// instead
$sql_request "select email, usuario, senha from usuarios where email = ? and senha = ?" ;
$query $this->db->query($sql_request, array($email$senha) ;

// or
$query $this->db->select('email, usuario, senha')->from('usuarios')->where(array('email' => $email'senha' => $senha))->get() ;

// it's more secure and think to clean your data when you receive it since your form validation with these methods 
$email $this->input->post('email_from_form'TRUE) ; // or 
$email $this->security->xss_clean$this->input->post('email_from_form')) ; // it's the same 

Theme © iAndrew 2016 - Forum software by © MyBB