I'm creating a web application in codeigniter 3.0. I have a problem accessing database object from codeigniter instance and use it as we do in model. My code is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$CI =& get_instance();
$CI->load->database();
function get_menu($slug)
{
$CI->db->select('menuid');
$CI->db->where(array('slug'=>$slug));
$query=$CI->db->get('menus')->row();
echo $menuid= $query->menuid;
$query="SELECT * FROM menu_items WHERE menuid=".$menuid;
write_menu($query);
}
This show me error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: CI
Filename: helpers/menu_helper.php
Line Number: 9
Backtrace:
File: C:\Users\Pradeep\Google Drive\control\helpers\menu_helper.php
Line: 9
Function: _error_handler
File: C:\Users\Pradeep\Google Drive\control\controllers\Welcome.php
Line: 19
Function: get_menu
File: C:\Users\Pradeep\Google Drive\index.php
Line: 292
Function: require_once
Is there any suitable method to do this ? Please suggest.