CodeIgniter Forums

Full Version: Help Me Understand Passing Data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]CIwannabe[/eluser]
Dear CodeIgniter Community,
I am very new to CodeIgniter but I am dedicated to working with it.

I have read the entire User Guide and almost everything is clear.

But the one area that leaves me a bit confused is performing database updates through a form. From what I understand, it works like this:
1. Data is pulled from the database via a model
2. The data is passed from the model to a controller, and then into a view, where it pre-populates form fields
3. Upon submission, the data moves from the view back through the controller, into the model
4. Once back in the model, ActiveRecord is used to actually change the database record

I don't quite get how to pass data between the model, controller, and view for this particular action.

Can anyone share a sample of a complete flow for this? Even seeing how a single field gets updated would help a lot!

Note: I have read the tutorials on NetTuts and they are great, but in the CRUD video they skip over the update function!

El Forum

[eluser]CIwannabe[/eluser]
Please?

I will be truly grateful for the help!

El Forum

[eluser]xerobytez[/eluser]
Take a look at this, this example if pretty rough and simple but it should give you the basic idea of how things work. Hope this helps.

Controller : /application/controllers/account.php
Code:
class Account extends CI_Controller {
public function edit() {
  //Load the model
  $this->load->model('account_model');
  
  //Record ID in the table
  $id = 2;
  
  //Check to see if the 'name' variable is present in POST data
  if (($name = $this->input->post('name', true)) !== false) {
   //Yup, so lets update the record in the database via the model
  
   $this->account_model->update_name($id, $name);
  } else {
   //Nope, fetch the name from the model
  
   $name = $this->account_model->fetch_name($id);
  }
  
  $this->load->view('account', array('name' => $name));
}
}

Model : /application/models/account_model.php
Code:
class Account_Model extends CI_Model {
public function __construct() {
  parent::__construct();
  
  //Set the table name
  $this->_table = 'table_accounts';
}

public function fetch_name($id) {
  $result = $this->db->select('name')->where('id', $id)->limit(1)->get($this->_table);
  
  
  //Return the name if a record match is found
  return ($result->num_rows()) ? $result->row()->name : false;
}

public function update_name($id, $name) {
  //Update the record and return the result from active record to the controller
  return $this->db->set('name', $name)->where('id', $id)->limit(1)->update($this->_table);
}
}

View : /application/views/account.php
Code:
<form method="post">
<label for="name">Name:</label>
&lt;input type="text" name="name" id="name" value="&lt;?php echo $name; ?&gt;" /&gt;

<br />
<br />

<button type="submit">Change Name</button>
&lt;/form&gt;

El Forum

[eluser]CIwannabe[/eluser]
xerobytez,
Thank you very much for jumping in to help! I really do appreciate it.

I've looked at numerous help/tutorial sites. They often discuss CRUD operations. But every time they get to "update," they either skip the details or do some kind of shortened version. It leaves a newbie rather lost.

I'll implement the code you suggested and maybe come back for more!

In the meantime, it would be terrific to hear from anyone else with more to add!