Welcome Guest, Not a member yet? Register   Sign In
Call function from "views"
#1

[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) ?
#2

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

[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.
#4

[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...
#5

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

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

[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.
#8

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

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB