Welcome Guest, Not a member yet? Register   Sign In
Some variables not being passed from Controller to View
#1

[eluser]xentova[/eluser]
this is my controller

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Edit_profile extends MY_Controller{
    
    function __construct(){
        parent::MY_Controller();
    }
    
    public function index(){
        $this->output->enable_profiler(TRUE);
        $this->load->library('form_validation');
        $this->load->model('user_model');
        $session_data = $this->session->userdata('logged_in');
        $data= array(
                'username' =>$session_data['username']
            );
        $username = $data['username'];
        $data = $this->user_model->edit_profile($username);
        
        $this->form_validation->set_rules('email_add', 'Email Address', 'trim|required|valid_email|callback_email_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('confirm_password', 'Password Confirmation', 'trim|required|matches[password]');
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        if(! $this->session->userdata('logged_in')){
            $this->load->view('pages/index');
            redirect('', 'refresh');
        }
        
        if($this->form_validation->run() == FALSE){
            $this->load->view('edit_profile/index', $data);
            $this->load->view('templates/footer');
        }
        else{
            $this->user_model->login('username', 'password');
            redirect('', 'refresh');
        }

    }
    
     public function username_check($str){
        
        $this->load->model('user_model');
        
        if ($this->user_model->checkUsername($str)){
        return true;
        }
        else{
            
        $this->form_validation->set_message('username_check', 'The Username already exist');            
        return false;
        
        }
    }
    
    public function email_check($str){
        
        $this->load->model('user_model');
        
        if ($this->user_model->checkEmail($str)){
        return true;
        }
        else{
            
        $this->form_validation->set_message('email_check', 'The Email address already exist');            
        return false;
        
        }
    }
    
    
    
}
?>

this is my model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model{
    
    public function __construct(){
        parent::__construct();
    }
        
    public function login($username,$password){
        
        $this -> db -> select('id, username, password, first_name, last_name');
        $this -> db -> from('users');
        $this -> db -> where('username', $username);
        $this -> db -> where('password', MD5($password));
        $this -> db -> limit(1);
        
        $query = $this -> db -> get();
        if($query -> num_rows() == 1){
            return $query->result();
        }
        else{
            return false;
        }
    }
    
    public function add_user(){
        $data=array(
        'username'=>$this->input->post('username'),
        'email_add'=>$this->input->post('email_add'),
        'password'=>md5($this->input->post('password')),
        'first_name'=>$this->input->post('first_name'),
        'last_name'=>$this->input->post('last_name')
        );
        $this->db->insert('users',$data);
    }
    
    function checkUsername($username){
        
        $this->db->where('username', $username);
        $query = $this->db->get('users');
        if ($query->num_rows() > 0){
            return false;
            }
            else{
            return true;
        }
    }
    
    function checkEmail($email_add){
        
        $this->db->where('email_add', $email_add);
        $query = $this->db->get('users');
        if($session_data['email_add'] = $email_add){
            return true;
        }
        if ($query->num_rows() > 0){
            return false;
            }
            else{
            return true;
        }
    }    
    
    public function edit_profile($username){
        
        $this -> db -> select('password, first_name, last_name, email_add');
        $this -> db -> from('users');
        $this -> db -> where('username', $username);
        $this -> db -> limit(1);
        
        $query = $this -> db -> get();
        if($query -> num_rows() == 1){
            return $query->result();
        }
        else{
            return false;
        }
    }
}
?>

this is my view
Code:
<?php echo validation_errors();
echo form_open("edit_profile");
?>

<h5>Email Address</h5>
&lt;input type="email" name="email_add" value="&lt;?php echo set_value('email_add', $email_add); ?&gt;" size="20" /&gt;

<h5>Password</h5>
&lt;input type="password" name="password" value="" size="20" /&gt;

<h5>Confirm password</h5>
&lt;input type="password" name="confirm_password" value="" size="20" /&gt;

<h5>First Name</h5>
&lt;input type="text" name="first_name" value="&lt;?php echo set_value('first_name', $first_name); ?&gt;" size="20" /&gt;

<h5>Last Name</h5>
&lt;input type="text" name="last_name" value="&lt;?php echo set_value('last_name', $last_name); ?&gt;" size="20" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>
  
&lt;?php echo form_close(); ?&gt;

PROBLEM: $email_add is undefined

im making a page for users to edit their profile. so when they go to the page, the values of each field should hv been set in by default. thing is, all the values i set_value are ok except for the $email_add variable. i've used var_dump() and profiler at controller and the variable seems to pass but it always shows up as undefined. i wil lsee this in my password field: <div after that it will show

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: email_add

Filename: edit_profile/index.php

Line Number: 6

" size="20" />
#2

[eluser]xentova[/eluser]
Problem Solved:
turns out my controller was getting the $data from MY_Controller which has the codes for my Login/Logout section in my header.

Any idea how I can go about NOT inheriting the $data from MY_Controller? I changed the Edit_profile controller to extend CI_Controller instead of MY_Controller and the variables aren't passing to the View
#3

[eluser]InsiteFX[/eluser]
When having problems passing $data to multiple views use it this way:
Code:
$this->load->vars($data);
$this->load->view('main_view');
#4

[eluser]xentova[/eluser]
okay thanks for the tip InsiteFX Smile




Theme © iAndrew 2016 - Forum software by © MyBB