Welcome Guest, Not a member yet? Register   Sign In
DB connection failing within instanciated objects
#1

[eluser]Unknown[/eluser]
I've been trying to call a controller from one of its fellows (for the same reason some of us do).

I kept getting the "undefined property" error. I found out browsing the forum that the solution was to use $CI =& get_instance(). I'm sorry to say it doesn't work for me. What it is happening is it creates an object of the class from which it's called instead.

Btw, I don't understand why $this->db doesn't work being called from a controller object (when instanciated).

Anyway, I ended up looking up the code to get_instance() and noticed CI was declared as global. (The function I saw returns CI or an OBJ(?)).

So I replaced $CI =& get_instance() with global $CI and it did the trick. Thought I'd post as I didn't see it mentioned while doing my research.
#2

[eluser]wiredesignz[/eluser]
Could you post some code showing how you used the global $CI variable

EDIT: The global $CI won't work if you upgrade to PHP5
#3

[eluser]Unknown[/eluser]
Sure. Here's the class that will insert all the views into the main template. There's just the view for the menu at the moment. I added a Menu class in the library.

class Welcome extends Controller {

function Welcome(){
parent::Controller();
}

function index()
{
//require_once('menu.php');
//$ct = new Menu();
$this->load->library('menu');
$data['menu']=$this->menu->index();
$this->load->view('maintemplate',$data);
}
}

And here's the beginning of the function in the Menu class

function index(){

global $CI;
//$CI = & get_instance(); throws the error
$query = $CI->db->query("select (*) from entries");
#4

[eluser]wiredesignz[/eluser]
Have you loaded the database library?
Is the Menu library a class or just a group of functions?

ie:
Code:
class Menu
{
    function index()
    {

    }
}




Theme © iAndrew 2016 - Forum software by © MyBB