Welcome Guest, Not a member yet? Register   Sign In
Help Me Understand Passing Data
#3

[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;


Messages In This Thread
Help Me Understand Passing Data - by El Forum - 09-17-2012, 07:11 AM
Help Me Understand Passing Data - by El Forum - 09-18-2012, 06:18 PM
Help Me Understand Passing Data - by El Forum - 09-18-2012, 06:42 PM
Help Me Understand Passing Data - by El Forum - 09-19-2012, 07:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB