Insert Update delete Using Codeigniter

Controller:-

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

class categorys extends CI_Controller
{
   
    function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('category');
    }
    public function category_view()
    {
        $data = array();
        //get messages from the session
        if ($this->session->userdata('success_msg')) {
            $data['success_msg'] = $this->session->userdata('success_msg');
            $this->session->unset_userdata('success_msg');
        }
        if ($this->session->userdata('error_msg')) {
            $data['error_msg'] = $this->session->userdata('error_msg');
            $this->session->unset_userdata('error_msg');
        }
        $data['categorys'] = $this->category->getRows();
        $data['title']    = 'Post Archive';
        //load the list page view
        $this->load->view('templates/header', $data);
        $this->load->view('templates/nav', $data);
        $this->load->view('categorys/category_view', $data);
        $this->load->view('templates/footer');
    }
   
    /*
     * Post details
     */
    public function view($id)
    {
        $data = array();
       
        //check whether post id is not empty
        if (!empty($id)) {
            $data['categorys'] = $this->category->getRows($id);
            $data['title']    = $data['categorys']['title']['data'];
           
            //load the details page view
            $this->load->view('templates/header', $data);
            $this->load->view('templates/nav', $data);
            $this->load->view('categorys/view', $data);
            //$this->load->view('templates/footer');
        } else {
            redirect('/categorys/category_view');
        }
    }
   
    /*
     * Add post content
     */
    public function add()
    {
        $data     = array();
        $postData = array();
        //if add request is submitted
        if ($this->input->post('postSubmit')) {
            //form field validation rules
            $this->form_validation->set_rules('category_name', 'post category_name', 'required');
            //prepare post data
            $postData = array(
                'category_name' => $this->input->post('category_name'),
            );
            //validate submitted form data
            if ($this->form_validation->run() == true) {
                //insert post data
                $insert = $this->category->insert($postData);
               
                if ($insert) {
                    $this->session->set_userdata('success_msg', 'Post has been added successfully.');
                    redirect('/categorys/category_view');
                } else {
                    $data['error_msg'] = 'Some problems occurred, please try again.';
                }
            }
        }
       
        $data['post']   = $postData;
        $data['title']  = 'Create Post';
        $data['action'] = 'Add';
       
        //load the add page view
        $this->load->view('templates/header', $data);
        $this->load->view('templates/nav', $data);
        $this->load->view('categorys/add-edit', $data);
        //$this->load->view('templates/footer');
    }
   
    /*
     * Update post content
     */
    public function edit($id)
    {
        $data = array();
       
        //get post data
        $postData = $this->category->getRows($id);
       
        //if update request is submitted
        if ($this->input->post('postSubmit')) {
            //form field validation rules
            $this->form_validation->set_rules('category_name', 'post category_name', 'required');
            //prepare post data
            $postData = array(
                'category_name' => $this->input->post('category_name'),
            );
           
            //validate submitted form data
            if ($this->form_validation->run() == true) {
                //update post data
                $update = $this->category->update($postData, $id);
               
                if ($update) {
                    $this->session->set_userdata('success_msg', 'Post has been updated successfully.');
                    redirect('/categorys/category_view');
                } else {
                    $data['error_msg'] = 'Some problems occurred, please try again.';
                }
            }
        }
       
       
        $data['post']   = $postData;
        $data['title']  = 'Update Post';
        $data['action'] = 'Edit';
       
        //load the edit page view
        $this->load->view('templates/header', $data);
        $this->load->view('templates/nav', $data);
        $this->load->view('categorys/add-edit', $data);
        //$this->load->view('templates/footer');
    }
   
    /*
     * Delete post data
     */
    public function delete($id)
    {
        //check whether post id is not empty
        if ($id) {
            //delete post
            $delete = $this->category->delete($id);
           
            if ($delete) {
                $this->session->set_userdata('success_msg', 'Post has been removed successfully.');
            } else {
                $this->session->set_userdata('error_msg', 'Some problems occurred, please try again.');
            }
        }
       
        redirect('/categorys/category_view');
    }
}



Model:-

            <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class category extends CI_Model{
    /*
     * Get posts
     */
    public function getRows($id = ""){
        if(!empty($id)){
            $query = $this->db->get_where('category', array('id' => $id));
            return $query->row_array();
            print_r($query);die;
        }else{
            $query = $this->db->get('category');
            return $query->result_array();
        }
    }
   
    /*
     * Insert post
     */
    public function insert($data = array()) {
        $insert = $this->db->insert('category', $data);
        if($insert){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }
   
    /*
     * Update post
     */
    public function update($data, $id) {
        if(!empty($data) && !empty($id)){
            $update = $this->db->update('category', $data, array('id'=>$id));
            return $update?true:false;
        }else{
            return false;
        }
    }
   
    /*
     * Delete post
     */
    public function delete($id){
        $delete = $this->db->delete('category',array('id'=>$id));
        return $delete?true:false;
    }

    public function checkVehicle_no($post_vehicle_no) {

    $this->db->where('vehicle_no', $vehicle_no);

    $query = $this->db->get('category');

    $count_row = $query->num_rows();

    if ($count_row > 0) {
      //if count row return any row; that means you have already this email address in the database. so you must set false in this sense.
        return FALSE; // here I change TRUE to false.
     } else {
      // doesn't return any row means database doesn't have this email
        return TRUE; // And here false to TRUE
     }
}

 

    public function get_route(){
        $result = $this->db-> select('id,route_code')-> get('category')-> result_array();

        $route_code = array();
        foreach($result as $r) {
          $route_code[$r['route_code']] = $r['route_code'];
        }
        $route_code[''] = 'Select Route No...';
        return $route_code;
    }
}


view:-

<div class="container">
    <?php if(!empty($success_msg)){ ?>
    <div class="col-xs-9">
        <div class="alert alert-success"><?php echo $success_msg; ?></div>
    </div>
    <?php }elseif(!empty($error_msg)){ ?>
    <div class="col-xs-9">
        <div class="alert alert-danger"><?php echo $error_msg; ?></div>
    </div>
    <?php } ?>
    <div class="row">
        <div class="col-xs-9">
            <div class="box">
            <div class="panel panel-primary ">
                <div class="panel-heading">Category<a href="<?php echo site_url('categorys/add/'); ?>" class="glyphicon glyphicon-plus pull-right" >AddNew</a></div>
                <div class="box-body">
                <div class="grid-view table-responsive" id="item-grid">
                <table id="example1" class="table table-bordered table-striped" id="item-grid">
                    <thead>
                        <tr>
                            <th width="5%">ID</th>
                            <th width="20%">Category Name</th>
                            <th width="15%">Action</th>
                        </tr>
                    </thead>
                    <tbody id="userData">
                        <?php if(!empty($categorys)): foreach($categorys as $post): ?>
                        <tr>
                            <td><?php echo ''.$post['id']; ?></td>
                            <td><?php echo $post['category_name']; ?></td>
                            <td>
                               <!--  <a href="<?php echo site_url('categorys/view/'.$post['id']); ?>" class="glyphicon glyphicon-eye-open"></a> -->
                                <a href="<?php echo site_url('categorys/edit/'.$post['id']); ?>" class="glyphicon glyphicon-edit"></a>
                                <a href="<?php echo site_url('categorys/delete/'.$post['id']); ?>" class="glyphicon glyphicon-trash" onclick="return confirm('Are you sure to delete?')"></a>
                            </td>
                        </tr>
                        <?php endforeach; else: ?>
                        <tr><td colspan="4">Post(s) not found......</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            </div>
            </div>
        </div>
    </div>
    </div>
</div>

Add:-

<div class="container">
    <div class="col-xs-12">
    <?php
        if(!empty($success_msg)){
            echo '<div class="alert alert-success">'.$success_msg.'</div>';
        }elseif(!empty($error_msg)){
            echo '<div class="alert alert-danger">'.$error_msg.'</div>';
        }
    ?>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <div class="panel panel-primary">
                <div class="panel-heading"><?php echo $action; ?> Category <a href="<?php echo site_url('categorys/category_view'); ?>" class="glyphicon glyphicon-arrow-left pull-right">GoView</a></div>
                <div class="panel-body">
                    <form method="post" action="" class="form">
                         <div class="form-group">
                            <label for="category_name">Category Name</label>
                            <input type="text" class="form-control" name="category_name" placeholder="Enter title" value="<?php echo !empty($post['category_name'])?$post['category_name']:''; ?>">
                            <?php echo form_error('category_name','<p class="help-block text-danger">','</p>'); ?>
                            </div> 
                        <input type="submit" name="postSubmit" class="btn btn-primary" value="Submit"/>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

Comments

Popular posts from this blog

Forgot password using codeigniter

Insert and Update or Edit Data using jQuery Dialogify with PHP Ajax

Facebook Login With Codeigniter