简体   繁体   中英

Populate form fields from database using Codeigniter

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.

You should pass a array to your view file. So replace:

$data = $this->entries_model->get_data();

with:

$data['entries_data'] = $this->entries_model->get_data();

and on your view file replace:

echo form_open('site/update',$data);?>

with:

echo form_open('site/update',$entries_data);?>

There are a few things going on here:

  1. $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.

  2. If 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')

  3. If 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);

Then, in the view:

first you need to pass data in proper way

replace

$data = $this->entries_model->get_data();

with:

$data['data'] = $this->entries_model->get_data();

for 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

<?php echo form_input('title',set_value((is_object($data)?$data->title:'')));?>

you have to do the same thing for your all form fields

Jcory has answered your question but let me add a little to it.

  1. In you model instead of return $query->result(); do this return $query->row(); this is because using returning a return object requires that you should loop through the resultset in your view
  2. Instead of $data = $this->entries_model->get_data(); do this $data['entry'] = $this->entries_model->get_data();
  3. In your view do this <?php echo form_input('title',set_value('title',$entry->title));?> I hope these changes may solve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM