[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>
<input type="text" name="name" id="name" value="<?php echo $name; ?>" />
<br />
<br />
<button type="submit">Change Name</button>
</form>