Welcome Guest, Not a member yet? Register   Sign In
Returning to update form after validation
#11

[eluser]Loquela[/eluser]
OK, I've got the model working with the perameter s you suggest but the form actions update_user() and within this function, there is no reference to the userid?
I think I'm back where I started sort of but this time I'm getting Undefined variable: user
Here's the complete code:
Controller:
Code:
<?php
class Users extends Controller {
    // Display create admin user form
    function create_admin ()
    {
        $data['title'] = 'Create admin users';
        $data['admins'] = $this->users_model->get_admin_users(); // get list of admin users
        $this->load->view('includes/header',$data);
        $this->load->view('admin/users/users_view',$data);
        $this->load->view('admin/users/create_admin_view',$data);    
        $this->load->view('includes/footer',$data);
    }
    // Add new user
    function add_user()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Retype password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if($this->form_validation->run()==FALSE)
        {
            $this->create_admin();
        }
        else
        {
        $data = array(
            'username'    =>$this->input->post('username'),
            'password'     =>$this->input->post('password'),
            'firstname' =>$this->input->post('firstname'),
            'lastname'     =>$this->input->post('lastname'),
            'email'        =>$this->input->post('email'),
            'dateadded'    =>$this->input->post('dateadded')
            );
            $this->users_model->create_user($data);
            $this->index();
        }    
    }
    // Display update user form
    function update_admin_form()
    {
        $data['title'] = 'Update admin user';
        $data['admins'] = $this->users_model->get_admin_users(); // get list of admin users
    
          if($this->uri->segment(4))
        {
            $data['user'] = $this->users_model->get_user($this->uri->segment(4));
        }
        $this->load->view('includes/header',$data);
        $this->load->view('admin/users/users_view',$data);
        $this->load->view('admin/users/update_admin_view',$data);    
        $this->load->view('includes/footer',$data);
    }
    // Update user details
    function update_user()
    {
        $data = array(
            'username'  =>$this->input->post('username'),
            'password'  =>$this->input->post('password'),
            'firstname' =>$this->input->post('firstname'),
            'lastname'  =>$this->input->post('lastname'),
            'email'     =>$this->input->post('email'),
            'updated'    =>$this->input->post('updated'),
        );
            if($this->uri->segment(4))
               {
               $data['user'] = $this->users_model->get_user($this->uri->segment(4));
            }
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Retype password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if($this->form_validation->run()==FALSE)
        {
            $this->update_admin_form($data);
        }
        else
        {
            $this->users_model->update_user($data);
            $this->update_admin_form($data);
        }
    }
}
Model:
Code:
<?php
class Users_model extends Model {
    
    // Get admin users (except Superadmin)
    function get_admin_users()
    {
        $query = $this->db->get_where('pp_users', array('userlevel' => 1));
        
        if($query->num_rows() > 0) {
            foreach($query->result() as $rows) {
                $data[] = $rows;
            }
            return $data;
        }
    }
    // Login Users
    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('pp_users');
        
        if($query->num_rows == 1)
        {
            return true;
        }
    }
    // Create new user
    function create_user($data)
    {
        $this->db->insert('pp_users',$data);
        return;
    }
    // Get user
     function get_user($user_id)
    {
        $query = $this->db->get_where('pp_users', array('userid' => $user_id));
        
        if ($query->num_rows() == 1)
        {
            $user = $query->row();
        }
        return $user;
    }
    // Update user details
    function update_user($user_id)
    {
        $this->db->where('userid', $user_id );
        $this->db->update('pp_users', $data);    
        return;
    }
}
Views:
users_view.php
Code:
<ul id="admin_user_list">
&lt;?php foreach($admins as $r): ?&gt;
<li>&lt;?php echo anchor('admin/users/update_admin_form/' . $r->userid, $r->username); ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>
<p>&lt;?php echo anchor('admin/users/create_admin', 'Create new admin user')?&gt;</p>
update_admin_view.php
Code:
[code]&lt;?php echo form_open('admin/users/update_user/');?&gt;
<p><label>Username</label>&lt;?php echo form_input('username',$user->username);?&gt;</p>
<p><label>Password</label>&lt;?php echo form_password('password');?&gt;</p>
<p><label>Re-type Password</label>&lt;?php echo form_password('re_password');?&gt;</p>
<p><label>First name</label>&lt;?php echo form_input('firstname', $user->firstname);?&gt;</p>
...


Messages In This Thread
Returning to update form after validation - by El Forum - 01-07-2010, 05:46 AM
Returning to update form after validation - by El Forum - 01-07-2010, 06:34 AM
Returning to update form after validation - by El Forum - 01-07-2010, 07:06 AM
Returning to update form after validation - by El Forum - 01-07-2010, 07:18 AM
Returning to update form after validation - by El Forum - 01-07-2010, 07:27 AM
Returning to update form after validation - by El Forum - 01-07-2010, 07:42 AM
Returning to update form after validation - by El Forum - 01-07-2010, 07:48 AM
Returning to update form after validation - by El Forum - 01-07-2010, 08:01 AM
Returning to update form after validation - by El Forum - 01-07-2010, 08:10 AM
Returning to update form after validation - by El Forum - 01-07-2010, 08:12 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:14 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:24 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:29 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:32 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:42 AM
Returning to update form after validation - by El Forum - 01-07-2010, 10:59 AM
Returning to update form after validation - by El Forum - 01-07-2010, 11:40 AM
Returning to update form after validation - by El Forum - 01-07-2010, 02:04 PM
Returning to update form after validation - by El Forum - 01-07-2010, 02:10 PM
Returning to update form after validation - by El Forum - 01-08-2010, 03:41 AM
Returning to update form after validation - by El Forum - 01-08-2010, 05:27 AM
Returning to update form after validation - by El Forum - 01-08-2010, 05:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB