3

I am working on session data where the email is loaded automatically when user enters new record in grocery crud table. On the top of page , the name /email of current user can be displayed by using (in view folder)

<?php echo $user->email;?>

However , when I tried using $user->email in the GC controller , it gives me errors even after assigning to a variable. Also tried changing the model by using set_userdata but still wouldnt work. The code is as follows

Error

Message: Undefined variable: user

Message: Trying to get property of non-object

Model

public function takeUser($username, $password)

{

  $this->db->select('*');
  $this->db->from('user');
  $this->db->where('username', $username);
  $this->db->where('password', $password);
  $query = $this->db->get();
  return $query->num_rows();

}

public function userData($username)
{

  $this->db->select('username');
  $this->db->select('name');
  $this->db->select('email');
  $this->db->where('username', $username);
  $query = $this->db->get('user');
  return $query->row();
}

Controller

public function program()
{
if($this->session->userdata('isLogin') == FALSE)
{
    redirect('login/login_form');
}
else
{

    $this->load->model('m_login');
    $user = $this->session->userdata('username');
    $data['level'] = $this->session->userdata('level');
    $data['user'] = $this->m_login->userData($user);
}
    $crud = new grocery_CRUD();
    $crud->set_table('program');
       $crud>columns('nama_program','kod_program','kos_program','rmk','rollingplan','user','email');
    $crud>field_type('nama_program','kod_program','kos_program','rmk','rollingplan','user','email');
    $crud>set_relation('rollingplan','rollingplan','nama_rollingplan');
    $crud->callback_field('user',array($this,'user_field'));
    $crud->callback_field('email',array($this,'email_field'));
    $crud->set_subject('Program Baru');    
    $crud->required_fields('nama_program');
    $output = $crud->render();
    $this->_example_output_prog($output);
}

function user_field($value = '', $primary_key = null)
{
    return '<input type="text" maxlength="50" readonly="readonly"     value="'.$this->session->userdata('username').'" name="user"     style="width:462px">';
}
function email_field($value = '', $primary_key = null)
{ 
    $mail=$user->email;
    return '<input type="text" maxlength="50" readonly="readonly"      value="'.$mail.'" name="email" style="width:462px">';
}
sleepyhead
  • 31
  • 5
  • You must check double this line: `$user = $this->session->userdata('username'); $data['level'] = $this->session->userdata('level'); $data['user'] = $this->m_login->userData($user);}` Cause you call $user in Controller and send $data['user'] to View, so View will understand that $user is `$data['user'] = $this->m_login->userData($user);`, **not is the user in Controller** `$user = $this->session->userdata('username');` – Vương Nguyễn Minh Mar 12 '17 at 04:42

2 Answers2

1

If you are using PHP version 5.4 or later (I think it works on PHP 5.3 but not sure), you can easily do that with anonymous functions and the use keyword. More specifically in your case you can do:

public function program() {
    ...
    $crud->callback_field('email',function ($value, $primary_key) use ($user) { 
        return '<input type="text" maxlength="50" readonly="readonly" value="'.$user.'" name="email" style="width:462px">';
    });
    ...
}

With the above way:

  1. The callback is within your function without searching it
  2. Your code is more clear
  3. It works as you are expecting :)
John Skoumbourdis
  • 3,041
  • 28
  • 34
0
$this->load->model('m_login');
$user='';
if($this->session->userdata('username'))
{
    $user = $this->session->userdata('username');
    $data['level'] = $this->session->userdata('level');
    $data['user'] = $this->m_login->userData($user);
}
else
{
    //session expired or not created.
}

initialize $user with blank and check if session variable exist or not.