Welcome Guest, Not a member yet? Register   Sign In
Best method for sending form variables to a model ?
#10

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


Messages In This Thread
Best method for sending form variables to a model ? - by El Forum - 07-22-2007, 03:44 PM
Best method for sending form variables to a model ? - by El Forum - 07-22-2007, 04:41 PM
Best method for sending form variables to a model ? - by El Forum - 07-23-2007, 11:14 AM
Best method for sending form variables to a model ? - by El Forum - 07-29-2008, 12:42 AM
Best method for sending form variables to a model ? - by El Forum - 07-29-2008, 02:26 AM
Best method for sending form variables to a model ? - by El Forum - 07-29-2008, 02:38 AM
Best method for sending form variables to a model ? - by El Forum - 07-29-2008, 02:49 AM
Best method for sending form variables to a model ? - by El Forum - 07-29-2008, 10:35 AM
Best method for sending form variables to a model ? - by El Forum - 11-25-2008, 12:05 PM
Best method for sending form variables to a model ? - by El Forum - 11-25-2008, 12:38 PM
Best method for sending form variables to a model ? - by El Forum - 11-25-2008, 12:50 PM
Best method for sending form variables to a model ? - by El Forum - 11-26-2008, 07:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB