0

I wrote the following 'insert code' in a way that I can use it many times. So without writing the table name in the model I could write the table name in the controller.

model function

function insertTable($table, $data) {
    $this->db->insert($table, $data);
    return $this->db->insert_id();
}

controller instruction

$insert = $this->Model_Action->insertTable('student',$student_data)

But I want to do the same in the following code as well. Is there a way to replace the p_id as $id?

Model

  function delete_by_id($id)
    {
        $this->db->where('p_id', $id);
        $this->db->delete('parent');
    }

Controller

 public function student_delete($id)
{
    $this->load->model('Model_Action');
    $this->Model_Action->delete_by_id($id);
    echo json_encode(array("status" => TRUE));
}

5 Answers 5

0

Yes you can. your model

function delete_by_id($column_name, $id){
    $this->db->where($column_name, $id);
    $this->db->delete('parent');
}

then use it in controller's student_delete methos like

$this->Model_Action->delete_by_id('p_id', $id);
0

If you want delete 'student' table row by ID, you can use to below code.

function delete_by_id($id) 
{
    $this->db->delete('student', array('id' => $id));
}

If you want delete different table row by ID:

function delete_by_id($table, $id) 
{
    $this->db->delete($table, array('id' => $id));
}
0
0

Well, you should use codeigniter Core Classes.

In folder hierarchy, there is a folder named core in which you can make parent controller and model.

You can make all functions of model which we are using in almost all models like for create, update, delete, read records in this parent model and inherit our app models from this model.

In our Controller, we just need to get model name and use these functions.

Note: I am using same way since 2018.

0

You can define Table_name in model for reusable insert , delete, edit methods.

You can use where conditions usig condition wise.

Ex.

function get($id='',$name='')
{
    if($id >0)
    {
        $this->db->where('id',$id);
    }
}
0

Controller:

Write required database table name to variable $table in controller. And pass the variable $table to model.

Model:

function insertTable($table, $data) {
    $this->db->insert($table, $data);
    return $this->db->insert_id();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.