Welcome Guest, Not a member yet? Register   Sign In
Calling a function from inside a view
#1

[eluser]Omaplie[/eluser]
Hi,

I really like this forum because people try to help each other.
That's why I post this new topic because I have a trouble.

In fact, I would like to call a function from a controller in a view.
In my view I have a « if » and if it's ok, launch my function in my controller.

I know that I need to respect the MVC but I don't know how to do

What I have done at first is to create a variable in my controller :
Code:
<?php $data['myfunction'] = $this->CallToOtherFunction(); ?>

and in my view :
Code:
<?php echo $myfunction; ?>

But my trouble is that I need to get parameters that are in my view.
I would like to call my function but with parameters and it's not possible
with the method with the variable.

Please give me a solution Smile

Thank you very much
#2

[eluser]Ayeyermaw[/eluser]
If this function is going to be called by more than one process then perhaps putting that function into a helper might be of better use to you
#3

[eluser]Omaplie[/eluser]
Hi,

Thank you for attention you give to my post.

I read that, the better solution is to use a helper like you say.
But I use helper just when I am using « anchor » and the « redirect ».

I don't really understand how that can works.
Can you give me an example please ?

Thank you a lot.
#4

[eluser]Ayeyermaw[/eluser]
Not sure what example would suit you best but one of my applications needs a list of students for a dropdown in more than one occasion and in more than one view. The function dropdown() is in my student_model
I created a helper - "students_helper.php" and in there I have the following code:

Code:
function dd_students()
{
  global $CI;
  return $CI->student_model->dropdown('id','name'); // Returns an array

}


I can call on this function from the controller or the view (need to make sure the helper is loaded either automatically or in the controller ofc).
To make it more complex I could add parameters to the function which I can pass in from the view or the controller.
Its not the best example but I hope it helps.

// EDIT: I didn't show the student_model loaded but hope you understood that would need to happen at some point too


#5

[eluser]Omaplie[/eluser]
Thank you for your fast reply.

Great I begin to understand.
Just some other questions :

Where did you put your function dd_students ? In the helper file ?
I would like to make some action in my function in the controller because as I see,
your function just call the model. I would like to create an array
with some value that I get in the view, is that possible ?

Many thanks for you help.
#6

[eluser]Ayeyermaw[/eluser]
Yes dd_students is in the helper file.

I'm not sure what your question is though. What value is in your view that isn't passed in from your controller?
Post your code - might help to see what you're trying to do.

#7

[eluser]Omaplie[/eluser]
Ok, I'll try, sorry I am not very accurate.

The code is quiet long, I'll try to make it shorter.
The fact is I try to display severals voting system on a page and
I use a foreach in a foreach.

Users can vote « Yes » or « No ».
When more than half users voted « Yes » (or « No »), I would like
to execute the function but I need to put in parameter the id_vote and
insert in the database some information that I get during the vote.

In my controller, the function that call the voting page :

Code:
<?php
    function list_vote_friend()
    {
        $data['friends_request'] = $this->unite_model->list_friend_request_model();
        
        $data['friends'] = $this->get_friends_unite();
        $data['users_online'] = $this->get_users_online();
        $data['uniteName'] = $this->uniteName();
        $data['numberInvit'] = $this->numberInvit();
        $data['list_votes_done'] = $this->list_vote_done();
        $data['nb_membre'] = $this->numberMembre();
        $data['nb_membre_vote'] = $this->numberMembreVote();
        $data['vote_positif'] = $this->nbr_vote_positif();
        $data['vote_negatif'] = $this->nbr_vote_negatif();
        
        $data['list_votes_friends'] = $this->unite_model->list_votes_friends_model();
        
        $layout_network['contenu'] = $this->load->view('list_vote_friend', $data, TRUE);
        $this->load->view('layout_network',$layout_network);
    }
?>

And in my view, the condition :

Code:
<?php

//IF VOTE NUMBER YES > NUMBER USERS /2
&lt;?php if($nb_membre/2 < $e): ?&gt;
&lt;?php echo ('La demande a bien été envoyé. Attendez une réponse de leur part.'); ?&gt;
//HERE CALL THE FUNCTION TO EXECUTE AN ACTION AND USE SOME PARAMETERS THAT I GET IN THE FOREACH

//IF VOTE NUMBER NO > NUMBER USERS /2 --&gt;                            
&lt;?php elseif($nb_membre/2 < $f): ?&gt;
&lt;?php echo ('La demande n a pas été envoyée étant donné le trop grand nombre de votes négatifs.'); ?&gt;

&lt;?php else: ?&gt;
// DISPLAY THE FORM

?&gt;
#8

[eluser]Ayeyermaw[/eluser]
so are you saying you need a function that works like so:

Code:
function set_vote_result($id, $result=TRUE){
  // Your code to save the vote result goes here
}

If the answer's yes then ofc you can put that function into a helper and call it from the view.
Obviously the id_vote would need to be passed into the view but I'm sure you are already doing that somehere.
Is that enough to go on or am I missing something?

#9

[eluser]Omaplie[/eluser]
Thank you a lot for all your help.

Sorry, I am not sur to understand everything.
Yes, I need a function like you say.

I'll put this function into the helper but no need
to call the controler ?

Is this method respect the MVC ?
It's not a trouble if it's not the case, it's just to know.

I'll try your solution by putting this function into the helper
but how can I call the function in the view, with parameters ?

Many thanks.
#10

[eluser]ojcarga[/eluser]
When you go to: www.yoursite.com/controller_name/method_name

You are executing a file named controller_name.php that has a method named method_name, right?

So in that file (controller_name.php), in the constructor (for example) you are going to load the helper file something like this
Code:
$this->load->helper('name');

Once the file (helper) has been loaded, you are going to be able to use any function in it...

Lets say I have a helper controlAccess_helper.php in /helpers/
I just load it this way in the constructor of my controller
Code:
$this->load->helper('controlAccess');

and then in the view or in the controller too, I can call the function
Code:
validateAccess();

Code:
function validateAccess(){
        $CI =& get_instance();
        $CI->load->model('/super/super_model');  
        
        ..........................        
    }


Here is the best place you can go for a better understanding:
http://ellislab.com/codeigniter/user-gui...lpers.html





Theme © iAndrew 2016 - Forum software by © MyBB