Welcome Guest, Not a member yet? Register   Sign In
Display Saved Database Answers in Form Issue
#1

[eluser]dhall[/eluser]
Greetings All!
I am fairly new to CI and coding in general so bare with me.
I have created a form that has several dropdowns along with blank fields for the user to enter their profile information.
I can save their answers to the database just fine but I seem to be having an issue with pulling and displaying them once saved.
I thought using the "set_value()" would do the trick, but its not working.
Any help would be great!

Here is an example of my setup for some fields. "last_name" is a blank field while "gender" is a dropdown. This is in my Controller.
Code:
$data['last_name'] = array (
    'name' => 'last_name',
    'id' => 'last_name',
    'value' => set_value('last_name',''),
    'style' => 'width:70%'
    );
    $data['gender'] = array (
    'name' => 'gender',
    'id' => 'gender',
    'selected' => set_value('gender',''),
    'style' => 'width:70%'
    );

This is where I call my Model and pass everything to the View. The "??" is where I am having my issue. What to put here so the fields will correspond to the correct question.
Code:
//get the users already saved data
    $data['??'] = $this->profile_model->get_profile();
    
    //display the page
    $data['title'] = 'Athlete Profile';  
    $data['main_content'] = 'profile_view';
    $this->load->view('templates/template_user', $data);

This is my Model. Each question in the form is setup as it's own field in the table.
Code:
function get_profile()
  {
    $query = $this->db->where('user_id',$this->session->userdata('user_id'))
                      ->get('u_profile');
    return $query->result();
  }

This is how those 2 fields look in the View.
Code:
echo form_label('Last Name: ').br(1);
  echo form_input($last_name).br(2);
  echo form_error('last_name','<p class="error">');

  echo form_label('Gender: ').br(1);
  echo form_dropdown('gender',$gender_options,'').br(2);
  echo form_error('gender','<p class="error">');

#2

[eluser]Aidy[/eluser]
Hey

Could you post your complete code for model, view and controller and will take a look.
#3

[eluser]dhall[/eluser]
The Controller
Code:
//initial page actions
  public function index()
  {
    
    //get the users already saved data
    $results = $this->profile_model->get_profile();
    
    //set fields
   $data['first_name'] = array (
    'name' => 'first_name',
    'id' => 'first_name',
    'value' => set_value('first_name',''),
    'style' => 'width:70%'
    );
   $data['last_name'] = array (
    'name' => 'last_name',
    'id' => 'last_name',
    'value' => set_value('last_name',''),
    'style' => 'width:70%'
    );
    $data['gender'] = array (
    'name' => 'gender',
    'id' => 'gender',
    'selected' => set_value('gender',''),
    'style' => 'width:70%'
    );
    $data['birth_year'] = array (
    'name' => 'birth_year',
    'id' => 'birth_year',
    'selected' => set_value('birth_year',''),
    'style' => 'width:70%'
    );
    $data['height'] = array (
    'name' => 'height',
    'id' => 'height',
    'value' => set_value('height',''),
    'style' => 'width:10%'
    );
    $data['weight'] = array (
    'name' => 'weight',
    'id' => 'weight',
    'value' => set_value('weight',''),
    'style' => 'width:10%'
    );
    $data['avatar'] = array (
    'name' => 'avatar',
    'id' => 'avatar',
    'value' => set_value('avatar',''),
    'style' => 'width:10%'
    );

    //get options for dropdown questions
    $this->load->model('dropdown_model');
    $data['gender_options'] = $this->dropdown_model->get_genders();
    $data['birthyear_options'] = $this->dropdown_model->set_birthyears();
    $data['level_options'] = $this->dropdown_model->set_levels();
    $data['experience_options'] = $this->dropdown_model->set_experience();
    $data['fitness_options'] = $this->dropdown_model->set_fitness();
    $data['method_options'] = $this->dropdown_model->set_training_methods();

    //display the page
    $data['title'] = 'Athlete Profile';  
    $data['main_content'] = 'profile_view';
    $this->load->view('templates/template_user', $data, $results);

  }

The Model
Code:
//pull the current information if already in database
  function get_profile()
  {
    $query = $this->db->where('user_id',$this->session->userdata('user_id'))
                      ->get('u_profile');
    return $query->results();
  }

The View
Code:
echo form_open('profile/save_profile');

  echo form_label('First Name: ').br(1);
  echo form_input($first_name).br(2);
  echo form_error('first_name','<p class="error">');

  echo form_label('Last Name: ').br(1);
  echo form_input($last_name).br(2);
  echo form_error('last_name','<p class="error">');

  echo form_label('Gender: ').br(1);
  echo form_dropdown('gender',$gender_options,'').br(2);
  echo form_error('gender','<p class="error">');
  
  echo form_label('Birth Year: ').br(1);
  echo form_dropdown('birth_year',$birthyear_options,set_value('birth_year',''),'').br(2);
  echo form_error('birth_year','<p class="error">');

  echo form_label('Height (inches): ').br(1);
  echo form_input($height).br(2);
  echo form_error('height','<p class="error">');
  
  echo form_label('Weight (lbs): ').br(1);
  echo form_input($weight).br(2);
  echo form_error('weight','<p class="error">');
  
  echo form_label('Profile Image: ').br(1);
  echo form_upload('userfile').br(2);
#4

[eluser]CroNiX[/eluser]
Because you're sending $results as the 3rd parameter in:
Code:
$this->load->view('templates/template_user', $data, $results);

$results should be part of $data, like $data['results'], since you're sending it to the view as a variable. The 3rd parameter to load::view() is supposed to be a boolean and if TRUE it returns the view as a variable instead of outputting it. It's FALSE by default so it outputs the view.
#5

[eluser]dhall[/eluser]
Yeah that $results in as the third parameter was me just trying whatever. It didn't work obviously.
I think what I need is each field in my database to be passed in $data.
Something like...

Code:
$data['first_name'] = 'answer for first_name';
$data['last_name'] = 'answer for last_name';
//and so on

But how do I do that without running a query for each answer?
#6

[eluser]CroNiX[/eluser]
2nd parameter of set_value() is the default value
Code:
$results = $this->profile_model->get_profile();
    
//set fields
$data['first_name'] = array (
  'name' => 'first_name',
  'id' => 'first_name',
  'value' => set_value('first_name', $results->first_name), //assuming it's 'first_name' in the db
  'style' => 'width:70%'
);
//etc...

Also, you'll want to return $query->row(); from your model since you are only getting a single result.
#7

[eluser]dhall[/eluser]
CroNiX,
THANKS! That works perfectly for fields that were a straight input.
What about fields that are a dropdown list?
I tried the following but it didn't work.

This was in my controller.
Code:
//set fields
$data['birth_year'] = array (
    'name' => 'birth_year',
    'id' => 'birth_year',
    'selected' => set_select('birth_year', $profile_data->birth_year),
    'type' => 'text'
    );

This was my view
Code:
//$birthyear_options is my array of years
echo form_label('Birth Year: ').br(1);
echo form_dropdown('birth_year',$birthyear_options).br(2);
echo form_error('birth_year','<p class="error">');

I thought I was on to something as I typed this post and tried changing the first parameter in form_dropdown to a variable like so...
Code:
echo form_dropdown($birth_year,$birthyear_options).br(2);
but that just gave me an error.

Thanks for the help!
#8

[eluser]CroNiX[/eluser]
set_select() only works if you are manually writing out the select element.
Controller:
Code:
//set fields
$data['birth_year'] = array (
    'name' => 'birth_year',
    'id' => 'birth_year',
    'type' => 'text'
);
$data['birth_year_default'] = $result->birth_year;  //Since you need to set this IN the form_dropdown() function, which is in your view, you'll need to pass this one value.
in the view
Code:
echo form_dropdown($birth_year, $birthyear_options, set_value('birth_year', $birth_year_default)).br(2);
#9

[eluser]dhall[/eluser]
Everything works great as long as the user has already saved data.
If no data exists already in the database table then I get the following error.

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profile.php
Line Number: 41

Where line 41 is

Code:
40 - $results = $this->profile_model->get_profile();
41 - $data['birth_year_answer'] = $results->birth_year;

All the code matches what you provided to the T.
This same error occurs for every input field.
How do I setup the code to pull answers if they exist, and leave blank if they don't?
Thanks for ALL the help!
#10

[eluser]CroNiX[/eluser]
You can do that several ways.

1) have separate create/edit forms. A create form wouldn't have those default values coming from the db, so you would just set the default values in that case.
2) when setting the values, test if the variable exists, if it doesn't set another default value, like an empty string or the first value of a select.
3) when pulling data from the db, check to see if you actually got results (meaning the person exists), if not, set up an array of your default values.
For #3, something like:
Code:
$results = $this->profile_model->get_profile();
if (count($results) == 0)
{
  $results = array(
    'first_name' => '',
    'last_name'  => '',
    'birth_year' => 0 //(to select the default value that has a key of 0 in your dropdown array, assuming you have a key with 0 for your default)
  );
}

Theres probably more ways, but off the top of my head...




Theme © iAndrew 2016 - Forum software by © MyBB