Thursday, December 27, 2018

CRUD Codeigniter

Blog_controller class


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blog_controller extends CI_Controller
{
    public function index()
    {
        $this->load->model('Blog_model');
        $data['blogs_result'] = $this->Blog_model->getBlogData();
        //print_r($data);
        $this->load->view('layout/header');
        $this->load->view('Blog/blog_view', $data);
        $this->load->view('layout/footer');
    }

    public function add()
    {
        $this->load->model('Blog_model');
        $data['blogs_result'] = $this->Blog_model->getBlogData();
   
        $this->load->view('layout/header');
        $this->load->view('Blog/add_view', $data);
        $this->load->view('layout/footer');
    }

    public function submit()
    {   
        $this->load->model('Blog_model');
        $result = $this->Blog_model->saveBlogData();
        if($result)
            $this->session->set_flashdata('success_msg', 'Record Inserted.');
        else
            $this->session->set_flashdata('error_msg', 'Error.');
        redirect("index.php/Blog_controller/index");
    }

    public function edit($id)
    {   
        $this->load->model('Blog_model');
        $data2['blogs_result'] = $this->Blog_model->getBlogDatabyID($id);

        $this->load->view('layout/header');
        $this->load->view('Blog/edit_view', $data2);
        $this->load->view('layout/footer');

        // if($result)
        //     $this->session->set_flashdata('success_msg', 'Record Inserted.');
        // else
        //     $this->session->set_flashdata('error_msg', 'Error.');
        // redirect("index.php/Blog_controller/index");
    }


    public function delete($id)
    {   
        $this->load->model('Blog_model');
        $result = $this->Blog_model->deleteBlogData($id);

        if($result)
            $this->session->set_flashdata('success_msg', 'Record Deleted.');
        else
            $this->session->set_flashdata('error_msg', 'Error.');
        redirect("index.php/Blog_controller/index");
    }
}

?>


Blog_model class

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blog_model extends CI_Model
{
   
    public function getBlogData()
    {
        $this->db->order_by('created_at', 'desc');
        $query = $this->db->get('blogs');

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    public function saveBlogData()
    {
        $field = array(
            'title'=> $this->input->post('txt_title'),
            'description'=> $this->input->post('txt_description'),
            'created_at'=> date('Y-m-d H:i:s')
        );
        $this->db->insert('blogs',$field);
        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    public function getBlogDatabyID($id)
    {
        $this->db->where('id', $id);
        $query = $this->db->get('blogs');

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    public function deleteBlogData($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('blogs');

        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

?>

blog_view class


            <h5>Blog List</h5><br>

            <?php

            if($this->session->flashdata('success_msg'))
            {
            ?>
                <div class="alert alert-success" role="alert">
                  <?php echo $this->session->flashdata('success_msg'); ?>
                </div>
            <?php
            }
            elseif ($this->session->flashdata('error_msg'))
            {?>
                <div class="alert alert-danger" role="alert">
                  <?php echo $this->session->flashdata('error_msg'); ?>
                </div>
            <?php
            }
            ?>

            <a href="<?php echo base_url('index.php/Blog_controller/add');?>" class="btn btn-info">Add New</a><br><br>
            <table class="table table-striped table-bordered">
                    <thead>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Description</th>
                        <th>Created At</th>
                        <th>Action</th>
                    </thead>
                    <tbody>

                        <?php
                            if($blogs_result)
                            {
                                foreach ($blogs_result as $blog)
                                    { ?>
                                        <tr>
                                        <td><?php echo $blog->id; ?></td>
                                        <td><?php echo $blog->title; ?></td>
                                        <td><?php echo $blog->description; ?></td>
                                        <th><?php echo $blog->created_at; ?></td>
                                        <td>
                                            <a href="<?php echo base_url('index.php/Blog_controller/edit/'.$blog->id);?>" class="btn btn-light">Edit</a>
                                            <a href="<?php echo base_url('index.php/Blog_controller/delete/'.$blog->id);?>" class="btn btn-danger">Delete</a>
                                        </td>
                                        </tr>
                              <?php }
                            }
                        ?>
                    </tbody>
            </table>
    

add_view class

            <h5>Add New</h5><br>
            <a href="<?php echo base_url('index.php/Blog_controller/index');?>" class="btn btn-info">Back</a><br><br>
   
            <form method="post" action="<?php echo base_url('index.php/Blog_controller/submit');?>" >
                  <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control" name="txt_title" id="title" placeholder="Blog Title..." required="true">
                  </div>
                  <div class="form-group">
                    <label for="description">Description</label>
                    <input type="text" class="form-control" name="txt_description" id="description" placeholder="Blog Description..." required="true">
                  </div>
                  <div class="form-group">
                      <input type="submit" class="btn btn-success" name="submitbtn" value="Submit">
                  </div>
            </form> 

edit_view class

            <h5>Edit Blog</h5><br>
            <a href="<?php echo base_url('index.php/Blog_controller/index');?>" class="btn btn-info">Back</a><br><br>
   

            <form method="post" action="<?php echo base_url('index.php/Blog_controller/update');?>" >
                  <input type="hidden" value="<?php echo $blogs_result->id; ?>" name="hidden_id" id="hidden_id" >
                  <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control" value="<?php echo $blogs_result->title; ?>" name="txt_title" id="title" required="true">
                  </div>
                  <div class="form-group">
                    <label for="description">Description</label>
                    <input type="text" class="form-control" value="<?php echo $blogs_result->description; ?>"  name="txt_description" id="description" required="true">
                  </div>
                  <div class="form-group">
                      <input type="submit" class="btn btn-success" name="updatebtn" value="Update">
                  </div>
            </form>

No comments:

Post a Comment