- Create your library, example “sample.php”
defined('BASEPATH') OR exit('No direct script access allowed');
class Sample
{
 protected $ci;
 public function __construct()
 {
        $this->ci =& get_instance();
 }
 public function debug($id)
 {
  $table = $this->ci->db->get_where('table', array('id' => $id))->num_rows();
  if (empty($table)) {
   $result = 'table is empty';
  } else {
   $result = $table;
  }
  return $result;
 }
}
- Load your library in controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Testing extends READY_Controller {
 public $page = 'Training';
 public function __construct()
 {
  parent::__construct();
  //Load Dependencies
  $this->load->library('sample');
 }
 public function index()
 {
  $data['result'] = $this->db->get('table')->result();
  $this->load->view('sampleView', $data); 
 }
}
- So, call function library in your views.
foreach ($result as $row) {
echo $this->sample->debug($row->id) . “\n\n\n”;
}
 
No comments:
Post a Comment