2

In my controller I get all the entries from Athletes table, names, surnames etc... Save them in the array and pass them to my view. In my view file I then list all the entries using foreach . All the Athletes are listed fine...

However I want to check every Athlete and see if they exist in the table My Team! So in my foreach loop in my view I use this function

$this->my_team_model->exist($this->session->userdata("id"), $athletes[$i][6]) === TRUE

First parameter of exist() function is user id I get from session, second parameter is id of Athlete from Athletes table ($athletes[$i][6]).

So you see I really need that second Athlete id parameter! But I can only get it when the Athletes are listed one by one in my view. But I know I shouldn't use functions and logic in my view files. How can I do this in my controller?

4
  • do you any special case if it doesn't exist Commented Jul 18, 2014 at 8:10
  • 1
    I'm not familiar with Codeigniter specifically, but why can't you do this logic in your controller, and send the newly filtered data to the view? Commented Jul 18, 2014 at 8:10
  • may be you could re-structure your db query itself to filter the results..! Commented Jul 18, 2014 at 8:11
  • 1
    if you have the data in a view, you must have had it in your controller. so you are actually not using a 'view variable in controller', you are using a 'controller variable in view' Commented Jul 18, 2014 at 8:14

1 Answer 1

1

you should be using that same foreach loop in your controller and get the required data there itself, append the data to the array you pass to the view:

$array = $this->some_model->all_the_data_of_atheletes();
foreach( $array as $key => $eachAthelete ){
    $exists = $this->model->call_to_function( $this->session->userdata("id"), $eachAthelete['id'] );
    $array[$key]['exists']  = $exists;
}
$this->load->view('some_view', $array);

Now, in your view you will get an exists element for every iteration. Hope it helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.