Skip to main content

Server side datatable using codeigniter 3

 


What is server side datatables?

Server side datatables are used to show large data sets. Advantage of server side datatables.

  • Data processing will be server side.
  • Show large data sets.
  • Server-side rendering is better for SEO than client-side rendering, as it can speed up the loading time of your page, improving user experience and helping your site rank higher in Google search results.
  • Using server-side rendering here has the benefit of keeping the information on the server-side and not delivering it to the client, even if you pull from relational databases or NoSQL stores.

HTML Code

<div class="tab-pane fade show active" id="tab10">

        <section class="main--content" style="padding:0px;" >

                <div class="panel">

                    <div class="records--list">

                        <table id="recordsListView" style="width:100%">

                            <thead>

                                <tr>

                                    <th>Id</th>

                                    <th>First Name</th>

                                    <th>Last Name</th>

                                    <th>Email</th>

                                    <th>Mobile</th>

                                    <th>Created Date</th>

                                    <th>Status</th>

                                    <th class="not-sortable">Actions</th>

                                </tr>

                            </thead>

                        </table>

                    </div>

                    </div>

            </section>

        </div>

Controller 

public function getIncomingCase(){

        $data = array();

        // Fetch member's records

        $jobData = $this->user_model->getRows($_POST);

 

        $i = $_POST['start'];

        foreach($jobData as $jobValue){

            $i++;

            $case_status = null;

            if($jobValue->status == 0){

                $case_status = '<span class="label label-warning">Action not initiated</span>';

            }

            $action = '<div class="dropleft">

                            <a href="#" class="btn-link" data-toggle="dropdown"><i class="fa fa-ellipsis-v"></i></a>

                            <div class="dropdown-menu">

                                <a href="'.base_url().'job/viewcase/'.$jobValue->id.'" class="dropdown-item">View</a>

                                <a href="#" class="dropdown-item">Reject</a>

                                <a href="#" class="dropdown-item">Archive</a>

                            </div>

                        </div>';

            $job = unserialize($jobValue->jobdata);

           

            $created = date( 'l Y/m/d H:i', strtotime($jobValue->createdAt));

            $data[] = array($jobValue->aid,

                $jobValue->salutation." ".$jobValue->firstname." ".$jobValue->lastname,

                $jobValue->mobile,

                $jobValue->natureofjob."<br>". $job['language_from'],

                "Null",

                $created,

                $case_status,

                $action);

        }

        $output = array(   

            "draw" => $_POST['draw'],

            "recordsTotal" => $this->user_model->countAll(),

            "recordsFiltered" => $this->user_model->countFiltered($_POST),

            "data" => $data,

        );

        echo json_encode($output);

    }


Model 


public function getRows($postData){

          $this->_get_datatables_query($postData);

          if($postData['length'] != -1){

            $this->db->limit($postData['length'], $postData['start']);

          }

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

          return $query->result();

      }

     

      /*

       * Count all records

       */

      public function countAll(){

        $this->db->from('job');

        return $this->db->count_all_results();

      }

     

      /*

       * Count records based on the filter params

       * @param $_POST filter data based on the posted parameters

       */

      public function countFiltered($postData){

        $this->_get_datatables_query($postData);

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

        return $query->num_rows();

      }

     

      /*

       * Perform the SQL queries needed for an server-side processing requested

       * @param $_POST filter data based on the posted parameters

       */

      private function _get_datatables_query($postData){

        $userid = $this->session->userdata('id');

        $this->column_search = array('CJ.id','CU.salutation','CU.firstname','CU.lastname','CU.mobile','CJ.aid','CJ.natureofjob','CJ.jobdata','CJ.docs','CJ.status','CJ.createdAt');

       

$this->db->select("CJ.id,CU.salutation,CU.firstname,CU.lastname,CU.mobile,CJ.aid,CJ.natureofjob,CJ.jobdata,CJ.docs,CJ.status,CJ.createdAt");

        // Set default order

        $this->order = array('CJ.id' => 'asc');

 

        $this->db->from('job as CJ');

        $this->db->join('job_assign as CJA','CJA.aid  = CJ.aid','LEFT');

        $this->db->join('users as CU','CU.id  = CJA.uid_to','LEFT');

        $this->db->where("CJ.userId", $userid);

       

        $i = 0;

          // loop searchable columns

        foreach($this->column_search as $item){

              // if datatable send POST for search

          if(isset($postData['search']['value'])){

                  // first loop

            if($i===0){

                      // open bracket

              $this->db->group_start();

              $this->db->like($item, $postData['search']['value']);

            }else{

              $this->db->or_like($item, $postData['search']['value']);

            }

 

                  // last loop

            if(count($this->column_search) - 1 == $i){

                      // close bracket

              $this->db->group_end();

            }

          }

          $i++;

        }

 

        if(isset($postData['order'])){

          $this->db->order_by($this->column_order[$postData['order']['0']['column']], $postData['order']['0']['dir']);

        }else if(isset($this->order)){

          $order = $this->order;

          $this->db->order_by(key($order), $order[key($order)]);

        }

      }


Comments

Popular posts from this blog

Codeigniter installation & setup: Codeigniter Club

                    Windows Environment   Install XAMPP or WAMP   Download and Unzip the package from Codeigniter.com   Extract all the documents in the server space (htdocs or www directory) Linux Environment  Download and Unzip the package from Codeigniter.com  Place the extracted folder in /var/www (in WAMP) or xampp/htdocs (XAMPP) Base URL   Go to application/config/config.php  Define base URL as $config['base_url'] = 'http://localhost/path/to/folder'; How to remove index.php from URL? go to root  create htaccess file  Add below code inside it  RewriteEngine on RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] Note: .htaccess code varies depending on the hosting server. Some hosting servers (e.g.: Godaddy) need to use an extr...

What is Codeigniter?

Welcome to Codeigniter Codeigniter is an Application development framework. Codeigniter framework is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. Current Version Codeigniter 4 Old Version Codeigniter 3 Model Model is used to access data from the database. If we need to get data from the database so we have to create a query (logic) for the database. View The view is used to create a view of the website. We can say, design of the website. We can create view (HTML, CSS, JavaScript, Ajax etc.). Users directly interact with the view of the website. Controller A controller is responsible for controlling the way that users interact with the MVC application. A controller is used to create logic for an application.  

Stripe payment gateway integration using cUrl - Codeigniter 3

  Stripe payment gateway integration using cUrl - Codeigniter 3 Sign up for a Stripe account at https://stripe.com/ Obtain Stripe api key Setup Codeigniter 3 Project Create a library for Stripe integration Create library inside libraries folder   application => libraries => Stripe_library.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Stripe_library { protected $CI; // Default Constructor public function __construct() { $this->CI =& get_instance(); // Load necessary libraries and helpers here if needed } // Create function for checkout public function create_checkout_session($amount, $currency, $success_url, $cancel_url) { $stripe_secret_key = 'Stripe_secret_key'; $api_url = 'https://api.stripe.com/v1/checkout/sessions'; $stripeamount = $amount * 100; $data = [ 'payment_method_types' => ['card'], ...