Wednesday, January 28, 2015

[CRUD] insert, update and delete data in codeigniter in a smart way…

Insert:
in your controller:
$DataInArray=array(‘db_field_name’=>value,’db_field_name’=>value);
$insertStatus = $this->model_name->insertInToTable(‘table_name’, $DataInArray);
in your model:
public function insertInToTable($tableName, $insertedData){
$this->db->insert($tableName, $insertedData);
$is_inserted = $this->db->affected_rows() > 0;
if($is_inserted) return TRUE;
else return FALSE;
}
********************************************
Update:
in your controller:
$updateData=array(‘db_field_name1’=>value,’db_field_name2’=>value);
$this->Setup_Model->updateTable(‘db_field_id’,$this->input->post(‘match_id’),’table_name’,$updateData);
in your model:
public function updateTable($fieldId, $matchId, $tableName, $data){
$this->db->where($fieldId, $matchId);
$is_updated=$this->db->update($tableName, $data);
if($is_updated) return $is_updated;
else return $is_updated;
}
******************************************
Delete:
in your controller:
$id=assign your id value here for deletion;
$deleteStatus=$this->model_name->deleteTableData(‘field_id’,$id,’table_name’);
in your model:
public function deleteTableData($fieldId, $matchId, $tableName){
$this->db->delete($tableName, array($fieldId => $matchId));
$is_deleted = $this->db->affected_rows() > 0;
if($is_deleted) return TRUE;
else return FALSE;
}
******************************************

No comments:

Post a Comment

Change priority of dual boot OS

Change priority of dual boot OS  (Windows and Linux): Go to your Linux OS, install Grub customizer. Then change priority by up and down arro...