⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Fatal error: Call to undefined function



ufodisko

ufodisko
  • profile picture
  • Member

Posted 29 September 2012 - 22:58 PM

I'm really confused as to why this is happening.

I have a controller named main.php and a view main_template.php

I created a custom function in main.php that returns the number of rows in a table. But when I goto main_template.php and insert

echo functionName();


I get the following error.

[quote][b]Fatal error[/b]: Call to undefined function functionName()[/quote]

What am I doing wrong?

victor

victor
  • profile picture
  • Member

Posted 30 September 2012 - 08:16 AM

hi! Echo this->functionName()

ufodisko

ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 13:01 PM

[quote name='victor' timestamp='1348992969' post='3698']
hi! Echo this->functionName()
[/quote]

That didn't work.

I'm now getting the following error

[quote]
[b] [b]Fatal error[/b]: Call to undefined method CI_Loader::getPropNum() in [b]C:\wamp\www\codeign\application\views\main_template.php[/b][/quote][/b]

victor

victor
  • profile picture
  • Member

Posted 30 September 2012 - 13:23 PM

Let me see your controller code.

ufodisko

ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 13:34 PM

I created a new_helper.php and placed my custom functions in it, the file is in 'libraries'

This is the code of the new helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
function test_method($var = '')
{
return $var;
}

function getPropNum() {
$query = $this->db->query('SELECT * FROM properties');
return $query->num_rows();
}
}


The first function is a test function and it works perfectly when called from the view.
echo test_method('Hello World');

However, the second function is the one that's making errors. It's a simple function to get the number of rows in a table.

Then I loaded the helper inside my controller main.php
$this->load->helper('new_helper');


Then inside my view main_template.php, I called my function

echo getPropNum();


PS: when I call

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 30 September 2012 - 14:05 PM

Hello [member='ufodisko']

Just to make sure that it will work for you try to add this lines at the beginning of your template (main_template.php) .


<?php
$ci = &get_instance();
$ci->load->helper('new_helper'); ?>


Don't worry to have as many times as you like the $this->load->helper(), library(), model(), ...e.t.c. as it will only load once. For example if you have already called the same $this->load->helper at your controller it is ok to call it to your template too, as it will not affect anything.

P.S. I moved the topic to the General topic as it is not related with grocery CRUD

ufodisko

ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 14:24 PM

I'm still getting the same error :(

[quote]
[b] [b]Fatal error[/b]: Call to undefined method CI_Loader::getPropNum() in [b]C:\wamp\www\codeign\application\views\main_template.php[/b] on line [b]85[/b][/b][/quote]


[b]Why would the test_method() function load but not the getPropNum() ?[/b]

victor

victor
  • profile picture
  • Member

Posted 30 September 2012 - 15:51 PM

why your helper is in "libraries"? remove your file to "helpers".

ufodisko

ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 16:04 PM

My helper is in /application/helpers/new_helper.php

victor

victor
  • profile picture
  • Member

Posted 30 September 2012 - 16:13 PM

if you call this function in controller is it work?

ufodisko

ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 16:17 PM

[quote name='victor' timestamp='1349021620' post='3708']
if you call this function in controller is it work?
[/quote]

How do I call it from the controller?