CodeIgniter Forums
Call function from "views" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Call function from "views" (/thread-19795.html)

Pages: 1 2


Call function from "views" - El Forum - 06-18-2009

[eluser]Mareshal[/eluser]
I am new in CodeIgniter, only a few days, but I already started to like it very much.

Here is my problem. How do i call a function without URL parameters ?

Let's say I have this:

Controller:
Code:
class Main extends Controller{    
    function diff($n1, $n2){
            $diff['res'] = $n1 - $n2;
        $this->load->view('main_diff', $diff);
    }
}

How do I call function diff from main_diff(view/main_diff.php) without this url: http://localhost/main/diff/12/24 and without $this->uri->segment(3) ?


Call function from "views" - El Forum - 06-18-2009

[eluser]Jondolar[/eluser]
You can call diff() from any function inside Main by $this->diff($n1,$n2);


Call function from "views" - El Forum - 06-18-2009

[eluser]gtech[/eluser]
you can default the variables, so that if .../12/24 are chopped off or 24 is chopped off it will default to 0
Code:
class Main extends Controller{    
    function diff($n1=0, $n2=0){
            $diff['res'] = $n1 - $n2;
        $this->load->view('main_diff', $diff);
    }
}

OR

you can temporary store the variables in the session.

OR

you can post the variables though a form as hidden values.

[edit]

if you are just calling the function then do as Jondolar said, I thought you wanted to call the function through a request.


Call function from "views" - El Forum - 06-19-2009

[eluser]Mareshal[/eluser]
I want simply to use that function in a "views" file without a form. I want to pass some post variables to the controller form view file without putting them into url or sessions.

P.S. Function calling inside that class is easy. But how to call it in another controller/class or in another model? I know I can call a model inside a controller like: $this->load->model('mod'); $this->mod->my_function(); . Does this apply in the other way in the model ? $this->load->controller('controller_name') . Or I have to write the function twice ?

Edit: Or I didn't understand fully the concept of MVC...


Call function from "views" - El Forum - 06-19-2009

[eluser]Thorpe Obazee[/eluser]
You want to load a controller within another controller?


Call function from "views" - El Forum - 06-19-2009

[eluser]Mareshal[/eluser]
Yes, and not only that. Please read my entire post. thank you


Call function from "views" - El Forum - 06-19-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="Mareshal" date="1245417583"]Yes, and not only that. Please read my entire post. thank you[/quote]

I've read it but I guess what you need is more of a helper.

EDIT:

I might have misunderstood what you want. It's probably best if you write the whole situation in one post?

It's getting hard to understand some parts:

Quote:I want to pass some post variables to the controller form view file without putting them into url or sessions.



Call function from "views" - El Forum - 06-19-2009

[eluser]Mareshal[/eluser]
Can you please explain the the solution to my problem?


Call function from "views" - El Forum - 06-19-2009

[eluser]Thorpe Obazee[/eluser]
points to previous post.


Call function from "views" - El Forum - 06-19-2009

[eluser]Mareshal[/eluser]
Ok, my mistake.

"I want to pass some post variables to the controller FROM view file without putting them into url or sessions." sorry.

I want to call a function from a controller or a model inside a "views" file without using a form.