Welcome Guest, Not a member yet? Register   Sign In
[solved] : controller functions calling from within fragment views, PHP4 -> PHP5
#1

[eluser]tomcode[/eluser]
Hi,

I used to call functions defined in the controller from within my views. Under PHP4 this works without any problems. Now I'm switching to PHP5 and I get:

Quote:Call to undefined method CI_Loader::insert_code()

in my Controller I've defined the function :
Code:
function insert_code()
{
    return 'Hi';
}

in the fragment view I call it with:
Code:
<?=$this->insert_code();?>

What to do ? I want to keep the function inside the controller (no helper or library loading)


Thanks, Thomas
#2

[eluser]Matthew Pennell[/eluser]
Return it as some data and then pass it to the view?

Controller
Code:
$data['code'] = $this->insert_code();
$this->load->view('myview', $data);

View
Code:
<?= $code ?>
#3

[eluser]tomcode[/eluser]
Sorry, did show my code example too simplictic. I'm passing information to the controller.
Each view file contains different parameters, so I cannot set the values in the controller.

The main idea is to keep the data out of the controller.
Code:
function insert_code($file = FALSE)
{
  if ($file)
  {
    $open = "<?php ";
    $file .= '.code.php';
    $code = $open .file_get_contents(APPPATH.'views/code/'.$file);
    $coloredstring =highlight_string($code,TRUE);
    $results[] = '<div class="code">';
    $results[] = str_replace('&lt;?php ','',$coloredstring);
    $results[] ='</div>';
    return implode("\r\n", $results);
  }
}

And in the fragment views i have
file 1
Code:
&lt;?=$this->insert_code('some_file_name');?&gt;
file 2
Code:
&lt;?=$this->insert_code('some_other_file_name');?&gt;

I also call the function several times per fragment view.
#4

[eluser]tomcode[/eluser]
I found the solution.

PHP5
Code:
&lt;?=$CI->insert_code('some_file_name');?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB