php - How to create a method where all crud operation will perform in codeigniter
924
This method does only save but i want it will do insert, update and delete in codeigniter
//Gallery Category CRUD Module
public function galleryCategory(){
if (!empty($_POST['gallery_cat_name'])){
$data = $this->input->post();
$data['gallery_cat_date'] = date("Y-m-d", strtotime(str_replace('/', '-', $this->input->post('gallery_cat_date'))));
//Data save
$response = $this->MyModel->save('gallery_category', $data);
if ($response) {
$sdata['success_alert'] = "Saved successfully";
}else{
$sdata['failure_alert'] = "Not Saved successfully";
}
$this->session->set_userdata($sdata);
redirect('back/galleryCategoryCreate');
}else{
$sdata['failure_alert'] = "Try again";
$this->session->set_userdata($sdata);
redirect('back/galleryCategoryCreate');
}
}
Answer
Solution:
You don't need to create Model with your queries for basic crud operations. CodeIgniter provides those as Query Builder class.
Selecting Data
The following functions allow you to build SQL SELECT statements.
Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:
Inserting Data
Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
Updating Data
Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:
Deleting Data
Generates a delete SQL string and runs the query.
The first parameter is the table name, the second is the where clause. You can also use the where() or or_where() functions instead of passing the data to the second parameter of the function:
Answer
Solution: