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');
    }
}
681

Answer

Solution:

You don't need to create Model with your queries for basic crud operations. CodeIgniter provides those as Query Builder class.

From CodeIgniter Documentation

Selecting Data

The following functions allow you to build SQL SELECT statements.

$this->db->get()

Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:

$query = $this->db->get('mytable');  // Produces: SELECT * FROM mytable

Inserting Data

$this->db->insert()

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:

$data = array(
        'title' => 'My title',
        'name' => 'My Name',
        'date' => 'My date'
);

$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

Updating Data

$this->db->update()

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:

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);
// Produces:
//
//      UPDATE mytable
//      SET title = '{$title}', name = '{$name}', date = '{$date}'
//      WHERE id = $id

Deleting Data

$this->db->delete()

Generates a delete SQL string and runs the query.

$this->db->delete('mytable', array('id' => $id));  // Produces: // DELETE FROM mytable  // WHERE id = $id

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:

$this->db->where('id', $id);
$this->db->delete('mytable');

// Produces:
// DELETE FROM mytable
// WHERE id = $id

You must refer the documentation once, there are a lot of helpers.

182

Answer

Solution:

<?php

 class Crud_Model extends CI_Model{
     
     public function get_where($table,$where){
    return  $data = $this->db->where($where)->get($table)->result_array();
    }

      public function select($table){
        return  $data = $this->db->get($table)->result_array();
        }

       public function insert($table,$data){
       return $query = $this->db->insert($table,$data);
      } 
      
        function delete($table,$where){
        return $del = $this->db->where($where)->delete($table);
        }
      
      public function edit($table,$where){
        return  $data = $this->db->where($where)->get($table)->result_array();
        }   
      
       public function update($table,$data,$where){
        return  $query = $this->db->where($where)->update($table,$data);
        }      
              
     }
?>

People are also looking for solutions to the problem: javascript - Execute code in the beginning of a new week

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.