Welcome Guest, Not a member yet? Register   Sign In
repopulate a form with data from table
#11

[eluser]osci[/eluser]
I commented on your original code to see differences
Code:
function edit()
{
    $this->load->model('user_model');
    
    if($this->input->post('Submit')){
    //do stuff with submited fields
    $data = array(
          'user_firstname' => $this->input->post('nome'),
      'user_lastname' => $this->input->post('cognome'),                  
      'user_username' => $this->input->post('username'),
      'user_email' => $this->input->post('email'),
      'user_corso' => $this->input->post('corso')
    );

    $this->user_model->edit_user($data);
    //$this->load->view('edit_user_form', $data);            
            
    } //else{
    //get details from database
    $data['single_user'] = $this->user_model->get_data_user();    
    $this->load->view('edit_user_form', $data);    
    
    //}
}

You were not returning the single_user when not submiting
#12

[eluser]ralf0909[/eluser]
thank you, now it works very fine Big Grin
#13

[eluser]DeaD SouL[/eluser]
Hi ralf0909,

[quote author="ralf0909" date="1306154708"]...
what's the differencre from "return $query;" and "return $query->result();" ?[/quote]

return $query
Quote:The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure. When retrieving data you will typically assign the query to your own variable, like this: .... read more

return $query->result();
Quote:This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this: .... read more
#14

[eluser]DeaD SouL[/eluser]
And if you check the fields names in your example.. you'll see that they're mismatch..

Anyway, try this, and read it carefully to understand how it worked:

Model:
Code:
function get_data_user( $user_id = NULL )
{
    // we used the $user_id parameter instead of $this->uri->segment(3), to avoid the double-check
    $this->db->where ('user_id', (int) $user_id );
    
    $query = $this->db->get( 'user' );
    
    if( $query->num_rows() > 0 )
    {
        $return = $query->result();
    }
    
    return isset( $return ) ? $return : FALSE;
}

function edit_user( $data = array(), $user_id = NULL )
{
    // just to make sure the $data is not empty and it is array
    if( ! empty( $data ) AND is_array( $data ) )
    {
        // we used the $user_id parameter instead of $this->uri->segment(3), to avoid the double-check
        $this->db->where('user_id', (int) $user_id );
        $return = $this->db->update( 'user', $data );
    }
    
    return isset( $return ) ? $return : FALSE;
}


Controller:
Code:
function edit()
{
    // making sure the user_id is integer
    if( ( $user_id = (int) $this->uri->segment(3) ) == 0 )
    {
        // otherwise, redirect the admin to the main users controller page
        redirect( 'users', 'refresh' );
    }
    
    // loading the model
    $this->load->model('user_model');
    
    // making sure the user_id is exist
    if( ( $data['single_user'] =  $this->user_model->get_data_user( $user_id ) ) === FALSE )
    {
        // otherwise, redirect the admin to the main users controller page
        redirect( 'users', 'refresh' );
    }
    
    // loading the validation library
    $this->load->library( 'form_validation' );
    
    // setting our rules
    $this->form_validation->set_rules( 'firstname', 'First Name', 'trim|strip_tags|required|xss_clean' );
    $this->form_validation->set_rules( 'lastname', 'First Name', 'trim|strip_tags|required|xss_clean' );
    $this->form_validation->set_rules( 'username', 'Username', 'trim|strip_tags|required|xss_clean' );
    $this->form_validation->set_rules( 'password', 'Password', 'trim|strip_tags|required|xss_clean' );
    $this->form_validation->set_rules( 'email', 'E-Mail', 'trim|strip_tags|required|xss_clean|valid_email' );
    
    // running the validation
    if ( $this->form_validation->run() == FALSE )
    {
        // if it fails, then show the form again
        $this->load->view('edit_user_form', $data);
    }
    else
    {
        // if everything seemed to be fine, then prepare the new data
        $data = array(
            'user_firstname' => $this->input->post( 'firstname', TRUE ),
            'user_lastname' => $this->input->post( 'lastname', TRUE ),
            'user_username' => $this->input->post( 'username', TRUE ),
            'user_password' => $this->input->post( 'password', TRUE ),
            'user_email' => $this->input->post( 'email', TRUE )
        );
        
        // updating the user
        if( $this->user_model->edit_user( $data, $user_id ) )
        {
            // if the user info has been updated successfully, then redirect the admin to the details page or whatever you want
            redirect( 'users/details/' . $user_id, 'refresh' );
        }
        else
        {
            // otherwise, re-show the form with the error
            $data['err_msg'] = 'Something went wrong with updating the user information !';
            $this->load->view('edit_user_form', $data);
        }
    }
}


View:
Code:
<?php
// view page
// setting & preparing the default values
$first_name    = array( 'name' => 'firstname', 'id' => 'firstname', 'value' => set_value('firstname', $single_user->user_firstname) );
$last_name    = array( 'name' => 'lastname', 'id' => 'lastname', 'value' => set_value('lastname', $single_user->user_last) );
$username    = array( 'name' => 'username', 'id' => 'username', 'value' => set_value('username', $single_user->user_username) );
$password    = array( 'name' => 'password', 'id' => 'password', 'value' => set_value('password', $single_user->user_password) );
$email        = array( 'name' => 'email', 'id' => 'email', 'value' => set_value('email', $single_user->user_email) );
?>

<?php echo validation_errors(); ?>
<?php /* if there is any error, we print it */ ;?>
<?php echo ( isset( $err_msg ) ? $err_msg : '' ); ?>
<?php echo form_open('users/edit'); ?><?php /* or you can use this: <?php echo form_open( $this->uri->uri_string() );?> */ ?>
    <?php /* printing the form elements */ ;?>
    &lt;?php echo form_label('First Name:', $first_name['id']) . form_input($first_name);?&gt;<br />
    &lt;?php echo form_label('Last Name:', $last_name['id']) . form_input($last_name);?&gt;<br />
    &lt;?php echo form_label('Username:', $username['id']) . form_input($username);?&gt;<br />
    &lt;?php echo form_label('Password:', $password['id']) . form_input($password);?&gt;<br />
    &lt;?php echo form_label('E-Mail:', $email['id']) . form_input($email);?&gt;<br />
    &lt;?=form_submit('submit', 'Update');?&gt;
&lt;?php echo form_close(); ?&gt;

To be honest I haven't tried that out.. but I guess it should work Wink

Don't forget to take a look at:
Form Validation
Form Helper
Finally, its always a good idea to read the CodeIgniter User Guide

I hope I helped you Smile
#15

[eluser]ralf0909[/eluser]
thank you very much dead soul! very helpful post Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB