For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Monday, March 23, 2015

Custom library in CodeIgniter

Custom library in CodeIgniter:

CodeIgniter is a MVC PHP Framework. Do you know this framework is very fast PHP framework. It is designed to create a dynamic, flexible & SEO friendly web application.
Now days CodeIgniter is very famous MVC framework. In past time when you work in Core PHP with OOP; you have developed own custom library which have many utilities functions which you uses in different Projects. This custom library save your time and remove complexity.

Now you are moving to modern programming languages like CodeIgniter  framework. Now the question is how to use that custom library in this CodeIgniter framework?


See below class with having functions which is your custom library:
<?php
// Class name is "Myclass". but you can change it with any name
class Myclass {
   // Declare local variable
   var $temp;
   // This is constructor of "Myclass" here you can initialize local variables etc.
    public function __construct(){
       // set local variable value with single space.
       $this->temp='';
    }
 
   // this is method will call after delete record in CodeIgniter Controller
    public function message_del() {
        // Add here your usefull code
        $this->temp='    Record Deleted.';
        return $this->temp;
    }
 
// this is method will call after edit record in CodeIgniter Controller
    public function message_edit() {
        // Add here your usefull code
           $this->temp='    Record Edited.';
        return $this->temp;
    }

   // this is method will call after add record in CodeIgniter Controller
    public function message_add() {
            // Add here your usefull code
        $this->temp='    Record Added.';
        return $this->temp;
    }
}
?>


Now how to use above class in CodeIgniter?  Follow below steps o how to use.
//Create Myclass.php file in codeigniter "application\libraries\"

//Myclass.php
<?php
// this is default CodeIgniter code Do not delete it
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Myclass {
   // Declare local variable , note that $temp variable will return from it's class's methods
   var $temp;
   var $CI;
   public function __construct(){
      // set CodeIgniter instance object here. but it is not necessary.
       $this->CI =&get_instance();
       // set local variable value with single space.
       $this->temp='';
    }
     // this is method will call after delete record in CodeIgniter Controller
    public function message_del() {
        // Add here your code which delete record from mysql database.
        $this->temp='    Record Deleted.';
        return $this->temp;
    }
 
// this is method will call after edit record in CodeIgniter Controller
    public function message_edit() {
         // Add here your code which edit record in mysql database.
           $this->temp='    Record Edited.';
        return $this->temp;
    }

   // this is method will call after add record in CodeIgniter Controller
    public function message_add() {
            // Add here your code which add record in mysql database.
        $this->temp='    Record Added.';
        return $this->temp;
    }
}
?>

Now how to use above class in any controller class
Create Category.php controller file in "application\controllers\" folder
Category.php


<?php

// this is default CodeIgniter code Do not delete it
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// This is the Controller class it must always extends with  CI_Controller
class Category extends CI_Controller {

// This the default constructor with same name as Category class
    function Category() {
        //it is necessary to call parent class's (CI_Controller) constructor
        parent::__construct();

        // this is the your Myclass class in Myclass.php file in "application\libraries\" folder
        // this is the way to load your custom class. Make sure it should be small case letter
        $this->load->library('myclass');
    }
    public function index() {
        // use own logic here, you can make empty it.
    }
    // suppose you want to use MyClass.php >message_add method here
    public function addrecord() {
        // this is the way to calling method message_add()
         $data['ans']=$this->myclass->message_add();
//now load "result_view" with ans data
        $this->load->view('result_view',$data);
    }
    // suppose you want to use MyClass.php method message_edit() here
    public function editrecord() {
        // this is the way to calling method message_edit()
        $data['ans']=$this->myclass-message_edit();
//now load "result_view" with ans data
       $this->load->view('result_view',$data);

    }
    // suppose you want to use MyClass.php method message_del() here
    public function deleterecord() {
        // this is the way to calling method message_del()
         $data['ans']=$this->myclass-message_del();
        //now load "result_view" with ans data
        $this->load->view('result_view',$data);
    }   
}
?>



Now how to use above class in any controller class
Create 
result_view.php  view file in "application\views\" folder
result_view.php

<?php
//now access the variable  "$ans"  which  set in  $data['ans']=$this->myclass-message_del();  
// and load it in the view as $this->load->view('result_view',$data);

if(isset($ans)) {
          echo $ans;

       }
?>


Share:

0 comments:

Post a Comment

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive