Welcome Guest, Not a member yet? Register   Sign In
give me 5! (calling a controller function from a view)
#1

[eluser]luisjose[/eluser]
Maybe this is too simple, but I don't know what I'am doing wrong.

this is the file controllers/test.php

<?php

class Test extends Controller {

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

function index()
{
$this->load->view('test_view');
}

function give_me_5()
{
return 5;
}

}
?>


and this is the file views/test_view.php

<?=$this->test->give_me_5(); ?>

but it doesn't work.

I'm calling the function correctly?

Sorry if I offend your inteligence with that simple question Wink
#2

[eluser]coolfactor[/eluser]
You can try:

Code:
$this->give_me_5();

Quite often the scope of the view is the same as the controller, at least under PHP 4.

With that said, it's usually not good practice for the view to call a controller function. You might consider a library or helper function instead.
#3

[eluser]ztinger[/eluser]
havent tried that way, but think the 'correct' way to do it in mvc is something like:

Code:
<?php

class Test extends Controller
{

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

    function index()
    {
        $data['giveme5'] = give_me_5();
        $this->load->view('test_view');
        
      
    }

    function give_me_5()
    {
        return 5;
    }

}
?>


and in your view:

Code:
<?=$giveme5?>
#4

[eluser]coolfactor[/eluser]
Yes, ztinger mentioned the best approach. Pass everything to the view that it needs. Don't have the view "ask" for information it needs.
#5

[eluser]luisjose[/eluser]
coolfactor,

$this->give_me_5();

doesn't work

ztinger,

After doing copy+paste of your code I have this error:

Fatal error: Call to undefined function give_me_5() in /var/www/CodeIgniter_1.5.3_dame5/system/application/controllers/test.php on line 13


Anyway, what I want is to call a function located in the controller from a view. It is the right way to do things? user defined functions must be in another place instead of the controller?
#6

[eluser]coolfactor[/eluser]
ztinger had a slight syntax error.

Should be:
Code:
$data['giveme5'] = $this->give_me_5();

The give_me_5() function is within the scope of your Test controller. Be careful about copy and pasting. Try to understand the gist of the solution rather than the actual code.

You also need to pass the $data array into the view:

Code:
$this->load->view('test_view', $data);
#7

[eluser]ztinger[/eluser]
It wasn't meant to code & paste. Most code examples in this forum aren't. You have do your homework too.

but try this:

Code:
<?php

class Test extends Controller
{

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

    function index()
    {
        $data['giveme5'] = $this->give_me_5();
        $this->load->view('test_view', $data);
        
      
    }

    function give_me_5()
    {
        return 5;
    }

}
?>

(edit: i see both CF and I posted at the same time Wink )
#8

[eluser]luisjose[/eluser]
Ok, thank you for your patien Smile

I can asume that you can only get data from a controller within a view? and not to call a function?
#9

[eluser]luisjose[/eluser]
I found a better way to call functions from the view. I put the function in the model instead of the controler.

So, here is the code. Maybe is useful for newbies like me Smile

models/test_model.php

Code:
class Test_model extends Model
{

    function Test_model()
    {
        parent::Model();
    }

    function give_me_5()
    {
        return 5;
    }

}


controller/test.php

Code:
class Test extends Controller
{

    function Test()
    {
        parent::Controller();
    $this->load->model('test_model');
    }

    function index()
    {
        $this->load->view('test_view');
    }

}

views/test_view.php

Code:
<?=$this->test_model->give_me_5() ?>
#10

[eluser]Crimp[/eluser]
Man, you really are taunting the MVC police.




Theme © iAndrew 2016 - Forum software by © MyBB