Welcome Guest, Not a member yet? Register   Sign In
some doubts about the coding standard
#1

[eluser]Reneesh T K[/eluser]
Hi,

I am using codeigniter for past few months. Anyway I have a simple doubt is I am doing it in the right way.. I have searched for a good coding practise.. but didn't get until now.

So please let me know is I am doing it in a good way.

Suppose view page I have a form with two fields.
When submitting the page it is going to the controller function saved_data. for eg the function is

class Catalog extends CI_Controller{

function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('catalog_model');
}
function save_data(){
$data['carrier'] = $this->input->post('carrier', TRUE);
$data['message'] = $this->input->post('message', TRUE);
$productId = $this->catalog_model->insert_db($data);
}
}

Then my model will look like
class Catalog_model extends CI_Model {

function __construct()
{
parent::__construct();
}
function insert_db($data){
$query = 'INSERT INTO product (carrier, message) VALUES
('.$this->db->escape($data['carrier']).', '.
$this->db->escape($data['message']) . ')';

$this->db->query($query);

$productId =$this->db->insert_id();
return $productId;
}
}

Now my question is that, do I need to assign the posted values to the $data array in the controller? I think this is losing my time as I need to store the posted values to an array in the controller and pass it to the model for insert to db.. I can directly get the posted values in the model.. then why I need to store it in data array and pass it to model.. Let me know your thinking about this..
#2

[eluser]cahva[/eluser]
Yeah, that way there is repetition. You can use $this->input->post in model straight but I would do it differently. In model I would have this:
Code:
function insert_db($data)
{
    if (empty($data))
    {
        return false;
    }
    
    $this->db->insert('product',$data);
    return $this->db->insert_id();
}
That way you can pass any data to your model(eg. if your schema changes) and dont have to repeat code.




Theme © iAndrew 2016 - Forum software by © MyBB