Home:ALL Converter>Populate form fields from database using Codeigniter

Populate form fields from database using Codeigniter

Ask Time:2013-03-29T01:06:54         Author:user2220713

Json Formatter

i am trying to populate two form fields from data that is retrieved from a database, in order for the user to update them. The table is called records and it is quite simple:

Record_ID
title
content

My model:

function get_data()
{
    $r = $this->uri->segment(3);

    $query = $this->db->get_where('records', array('Record_ID' => $r));
    return $query->result();
}

My controller:

function set_values()
{
    $data = $this->entries_model->get_data();
    $this->load->view('update_view', $data);                       
}

and my update record view:

<?php

echo form_open('site/update',$data);?>
Title:
<?php echo form_input('title',set_value('title'));?>
Content:
<?php echo form_input('content',set_value('content'));
echo form_submit('submit', 'Submit');?>

<?php echo form_close();?>

The problem is that i get the following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/update_view.php

Line Number: 10

My question is twofold:

  1. How do i access this data in my view form and
  2. how do i populate the respective fields with it.

I am new to Codeigniter, my questions may look simplistic but any help would be appreciated. Thanks in advance.

Author:user2220713,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15687928/populate-form-fields-from-database-using-codeigniter
John Corry :

There are a few things going on here:\n\n\n$data is an array or object that is passed to a view. It's ELEMENTS are then available as variables in the view. So, $data['myelement'] = 'somevalue' in the controller would be accessed as $somevalue in the view.\nIf you pass a 2nd parameter to the form_open() method, it is expected to be a key/value pair of attributes for the tag that will be generated. like, array('class' => 'form_class', 'id' => 'form_id')\nIf you want to set the values of your form inputs, use the view helper function set_value(). In your case, use the controller to set elements in the $data array you'll pass to the view. $data['form_values'] = array('title' => $title, 'content' => $content);\n\n\nThen, in the view:",
2013-03-28T17:39:44
sv_adriano :

You should pass a array to your view file. So replace:\n\n$data = $this->entries_model->get_data();\n\n\nwith:\n\n$data['entries_data'] = $this->entries_model->get_data();\n\n\nand on your view file replace:\n\necho form_open('site/update',$data);?>\n\n\nwith:\n\necho form_open('site/update',$entries_data);?>\n",
2013-03-28T17:37:46
umefarooq :

first you need to pass data in proper way \n\nreplace\n\n$data = $this->entries_model->get_data();\n\nwith:\n\n$data['data'] = $this->entries_model->get_data();\n\n\nfor setting value in set_value you need to do the in-line condition check to check either data is an object or not if object then put value other wise just empty\n\n<?php echo form_input('title',set_value((is_object($data)?$data->title:'')));?>\n\n\nyou have to do the same thing for your all form fields ",
2013-03-28T17:47:32
yy