CodeIgniter Forums

Full Version: Library, Controller, View issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]adamp1[/eluser]
Right I really need someone to give this a go becasue its driving me insane. I have a very very simple test setup, a library, a controller and a view. All I am trying to do is load the library in the controller, set a value and then display it in the view.

Now I have done lots of CI development but I for some reason can't get this to work. Am I missing something simple?

The Library
Code:
<?php
class MyClass
{
    var $myval = 0;
    
    function MyClass()
    {
        print "Value = " . $this->myval . ", Expecting 0<br/>";
    }
}
?&gt;

Controller
Code:
&lt;?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();

        $this->load->library('myclass');
        
        $this->myclass->myval++;
        print "Value = " . $this->myclass->myval . ", Expecting 1<br/>";
    }

    function index()
    {
        $this->myclass->myval++;
        print "Value = " . $this->myclass->myval . ", Expecting 2<br/>";

        $this->load->view('test');
    }
}
?&gt;

View
Code:
&lt;?php
$this->myclass->myval++;
print "Value = " . $this->myclass->myval . ", Expecting 3<br/>";
?&gt;

Please could someone have a look, as said this is driving me insane, its so simple but it just doesn't work. The output I get for some reason is the following:
Quote:Value = 0, Expecting 0Value = 1, Expecting 1
Value = 2, Expecting 2
Value = 2, Expecting 3

As you can see from the output, first of why is the second output not on its own line??
Also why when we do the last inc in the view does it seem to loose count. Its like the myclass values don't persist across from the controller into the view.

El Forum

[eluser]Dam1an[/eluser]
Hmmm... I tried to recreate youre problem locally, and I couldn't
Even when I tried to break it, the only way I managed to was to increment it inline, which I expected to give the wrong result (2 when it expected 3) as it post increments

Is this a clean install? (Not sure why that would make a differance)

El Forum

[eluser]adamp1[/eluser]
Yes clean install 1.7.1. So you get the correct values? I have looked at it for ages and just don't get it.

El Forum

[eluser]adamp1[/eluser]
Now I have just run the test on another web server I have access to and it works fine. Least I know I'm not going insane, but for the reason its not working on my dev server I am at a lost end.

El Forum

[eluser]Dam1an[/eluser]
Wierd
Now sure what sort of settings would be differant, but diff the php.ini and httpd.conf files, see if anything stands out as suspect

El Forum

[eluser]TheFuzzy0ne[/eluser]
I'd be interested to know if making that class into a static class would help. Just include the file, and then try:
Code:
myclass::myval++;

Another suggestion might be to do this in the view:
Code:
$CI =& get_instance();
$CI->myclass->myval++;

Whilst I'm not entirely sure what's going on, I'm sure at least one of those solutions will work.