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

[eluser]ralf0909[/eluser]
i'm working on crud operations and i have a problem with the edit form.

i need to extract a single row from the table user, and make the fields appear in the values of the form. with traditional procedural php it was very simple, but with CI it's a bit difficult for me. so i tried this but i need help:

model
Code:
function get_data_user()
{
    $this->db->where('user_id', $this->uri->segment(3));
    $query = $this->db->get('user');
    return  $query->result();
}

function edit_user($data)
{
    $this->db->where('user_id', $this->uri->segment(3));
    $this->db->update('user', $data);    
}

controller
Code:
function edit()
{
    $this->load->model('user_model');
        
    //here the code to populate the fields with actual data
    $data['single_user'] =  $this->user_model->get_data_user();    
    
    
    //this is for update the actual data with new data
    $data = array(
      'user_firstname' => $this->input->post('firstname'),
      'user_lastname' => $this->input->post('lastname'),
      'user_username' => $this->input->post('username'),
      'user_password' => $this->input->post('password'),
      'user_email' => $this->input->post('email')        
    );

    $this->user_model->edit_user($data);
    $this->load->view('edit_user_form', $data);
}

and the view:
Code:
<?php echo validation_errors(); ?>
     <?php echo form_open('users/edit'); ?>
      
     <p>
         <label for="name">Name</label>
         &lt;input type="text" name="firstname" value="&lt;?php $data['single_user']-&gt;user_firstname; ?&gt; " />
     </p>
    
...... etc other fields

&lt;?php echo form_close(); ?&gt;

someone can help me?
#2

[eluser]fivefinger-bd[/eluser]
Instead of this
Code:
&lt;input type="text" name="firstname" value="&lt;?php $data['single_user']-&gt;user_firstname; ?&gt; " />

Try this:
Code:
&lt;input type="text" name="firstname" value="&lt;?php $single_user-&gt;user_firstname; ?&gt; " />
#3

[eluser]ralf0909[/eluser]
the error is the same:
Message: Undefined variable: single_user
and
Message: Trying to get property of non-object

(referred to the line &lt;input type="text" name="firstname" value="&lt;?php $single_user-&gt;user_firstname; ?&gt; " /> )
#4

[eluser]Ellli[/eluser]
well, you overwrite $data['single_user'] with $data = array(...);

add an indec to your $data after update like this $data['new_data']:
Code:
function edit()
{
    $this->load->model('user_model');
        
    //here the code to populate the fields with actual data - added an index
    $data['single_user'] =  $this->user_model->get_data_user();    
    
    
    //this is for update the actual data with new data
    $data['new_data'] = array(
      'user_firstname' => $this->input->post('firstname'),
      'user_lastname' => $this->input->post('lastname'),
      'user_username' => $this->input->post('username'),
      'user_password' => $this->input->post('password'),
      'user_email' => $this->input->post('email')        
    );

    $this->user_model->edit_user($data);
    $this->load->view('edit_user_form', $data);
}


and in view if you want to get value of $data['single_user']->... write $single_user and if you want to get value of $data['new_data'] write $new_data->...
#5

[eluser]ralf0909[/eluser]
just another little help, now the error is:

Error Number: 1054
Unknown column 'single_user' in 'field list'
UPDATE `user` SET `single_user` = Array, `new_data` = Array WHERE `user_id` = '50'

seems that single_user is considered a column, i'm searching a solution but i don't know how to resolve
#6

[eluser]Ellli[/eluser]
why are you updating table in database before form is submitted? I cant see any logic in it.

You can create two functions, one to get details from database and show it in form, and second one for actions after form is submitted. Or in one function use if statemant to decide if you wan't get details from database and show it, or just update it.

something like:
Code:
if($this->input->post('your_submit_name')){
  //do stuff with submited fields
}else{
  //get details from database
}
or use form_validation class where you can also validate user inputs.
#7

[eluser]InsiteFX[/eluser]
Code:
&lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname', $single_user); ?&gt;" /&gt;

InsiteFX
#8

[eluser]fivefinger-bd[/eluser]
[quote author="InsiteFX" date="1306099711"]
Code:
&lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname', $single_user); ?&gt;" /&gt;

InsiteFX[/quote]

This method also let you use error displaying.
#9

[eluser]ralf0909[/eluser]
[quote author="Ellli" date="1306097626"]why are you updating table in database before form is submitted? I cant see any logic in it.

You can create two functions, one to get details from database and show it in form, and second one for actions after form is submitted. Or in one function use if statemant to decide if you wan't get details from database and show it, or just update it.

something like:
Code:
if($this->input->post('your_submit_name')){
  //do stuff with submited fields
}else{
  //get details from database
}
or use form_validation class where you can also validate user inputs.[/quote]

so what i need to do?

i want to show the form with old data inside (or near the fields, is the same), if i change data in the fields and click on submit button, new data will saved in the database.

i'm new to codeigniter and object oriented programming.
i tried the if command but it doesn't show anything, i'm doint it wrong.

i think the problem is the function to extract data, tell me if is correct:
in the model i use a select on the row with the user_id of the user, and then i use a get to retrieve data. then i save the data on a variable named $query and put a "return $query;" right?
what's the differencre from "return $query;" and "return $query->result();" ?
#10

[eluser]ralf0909[/eluser]
i tried this, now works but i still get an error message:

controller:
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);    
    
    }
}

view:
Code:
&lt;?php foreach($single_user as $row) : ?&gt;
    
     &lt;?php echo validation_errors(); ?&gt;
     &lt;?php echo form_open('users/edit/'.$row->user_id.''); ?&gt;
    
     <p>
         <label for="id">ID</label>
         &lt;?php echo $row->user_id; ?&gt;
     </p>
      
     <p>
         <label for="name">Name</label>
         &lt;input type="text" name="nome" value="&lt;?php echo $row-&gt;user_firstname; ?&gt;" />
     </p>
    ..... etc other fields....

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

the single user data is shown in the fields, if i change something and click on submit, it works, but i get this error message from the view:

Severity: Notice
Message: Undefined variable: single_user

and

Severity: Warning
Message: Invalid argument supplied for foreach()




Theme © iAndrew 2016 - Forum software by © MyBB