[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">
<?php foreach($admins as $r): ?>
<li><?php echo anchor('admin/users/update_admin_form/' . $r->userid, $r->username); ?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('admin/users/create_admin', 'Create new admin user')?></p>
update_admin_view.php
Code: [code]<?php echo form_open('admin/users/update_user/');?>
<p><label>Username</label><?php echo form_input('username',$user->username);?></p>
<p><label>Password</label><?php echo form_password('password');?></p>
<p><label>Re-type Password</label><?php echo form_password('re_password');?></p>
<p><label>First name</label><?php echo form_input('firstname', $user->firstname);?></p>
...
[eluser]flaky[/eluser]
put here the complete error message
[eluser]flaky[/eluser]
update_admin_view.php
in the beggining
change it to
Code: <?php echo form_open('admin/users/update_user/' . $user->userid);?>
[eluser]Loquela[/eluser]
Thanks Flaky!
Basically the variable $user is not being defined:
Code: A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: users/update_admin_view.php
Line Number: 2
Code: A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: users/update_admin_view.php
Line Number: 2
[eluser]flaky[/eluser]
change
Code: function update_admin_form()
{
to
Code: function update_admin_form($user_id=null)
{
then change
Code: if($this->uri->segment(4))
{
to
Code: if($user_id != null)
{
$data['user'] = $this->users_model->get_user($user_id);
}
this
Code: 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);
}
change to
Code: 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('admin/users/update_admin_view',$data);
}
$this->load->view('includes/header',$data);
$this->load->view('admin/users/users_view',$data);
$this->load->view('includes/footer',$data);
}
[eluser]Loquela[/eluser]
Thanks flaky,
I'm really sorry that's confused me. What do I need to change in the update_admin_form function?
Your last change here seems to overwrite the prvious one?
Thanks,
[eluser]Loquela[/eluser]
Code: <?php echo form_open('admin/users/update_user/' . $user->userid);?>
This actually does get rid of the errors and on submit I am returned to the form and the values from the selected row are inserted!
HOWEVER, the form does not update the record if I change the valuse in the fields and the validation doesn't work!
I THINK we're getting somewhere but not quite there yet...
Anymore thoughts flaky?
Cheers
[eluser]flaky[/eluser]
do after update_user to check if the values have been updated in the array
Code: else
{
$this->users_model->update_user($data);
print_r($data);
$this->update_admin_form($data);
}
[eluser]flaky[/eluser]
found the little bug
in the controller you have it like this
Code: else
{
$this->users_model->update_user($data);
$this->update_admin_form($data);
}
while in the model
Code: function update_user($user_id)
{
$this->db->where('userid', $user_id );
$this->db->update('pp_users', $data);
return;
}
change them to this
in the controller
Code: else
{
$this->users_model->update_user($this->uri->segment(4), $data);
$this->update_admin_form($data);
}
in the model
Code: function update_user($user_id, $data)
{
$this->db->where('userid', $user_id );
$this->db->update('pp_users', $data);
return;
}
[eluser]Loquela[/eluser]
Thanks a lot flaky!
I'll get back to you as soon as I get a chance to look at this.
|