[eluser]crumpet[/eluser]
To above : i think your model needs to be in lower case like this $this->get_freebies->method();
On the subjet:
If you name consistently through your application so your form fields and database fields are the same then you can do this:
view:
Code:
<input type="text" name="var1"></input>
<input type="text" name="var2"></input>
<input type="text" name="var3"></input>
controller:
Code:
//Validate of course.. then when data is santized:
$this->load->model('Product_Model', 'products', TRUE);
if($this->products->save($_POST)){
redirect('successPage');
}else{
die('failed to save product');
}
model:
Code:
function save($data){
$product = (object) $data;
//determine whether we are creating a new entry or saving over an old one
return isset($data->productID) ? $this->_update($product) : $this->_create($product);
}
function _create($data){
//if we are successful return the new id otherwise return false
return $this->db->insert('products', $data) ? $this->db->insert_id() : FALSE;
}
function _update($data){
$this->db->where('productID', $data->productID);
return $this->db->update('products', $data);
}
That is for something like adding a new product with many fields.. but for login with only two fields I thing the best thing to do is this:
Controller
Code:
function login(){
//validaiton code...
if($this->validation->run(){
//do stuff
}
else{ //lets log in
$this->load->model('Users_Model', 'users', TRUE);
if ($this->users->login($_POST['username'], md5($_POST['password']))){
//set cookies in the controller
//success page
}
else{
die('failed');
}
}
}
Model
Code:
function login($username, $password){
$password = md5($password);
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
return $this->db->num_rows() > 0;
}