CodeIgniter Forums
[solved] : controller functions calling from within fragment views, PHP4 -> PHP5 - 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: [solved] : controller functions calling from within fragment views, PHP4 -> PHP5 (/thread-3442.html)



[solved] : controller functions calling from within fragment views, PHP4 -> PHP5 - El Forum - 10-02-2007

[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


[solved] : controller functions calling from within fragment views, PHP4 -> PHP5 - El Forum - 10-02-2007

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



[solved] : controller functions calling from within fragment views, PHP4 -> PHP5 - El Forum - 10-02-2007

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


[solved] : controller functions calling from within fragment views, PHP4 -> PHP5 - El Forum - 10-15-2007

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

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