CodeIgniter Forums
undefined function anchor() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: undefined function anchor() (/showthread.php?tid=83)



undefined function anchor() - davy_yg - 11-04-2014

Fatal error: Call to undefined function anchor() in C:\xampp\htdocs\CI202-CRUD\application\controllers\welcome.php on line 13

controllers/welcome.php

PHP Code:
class Welcome extends CI_Controller {
    function 
__construct()
    {
    
//    parent::__construct();
    //    $this->load->helper(array('html','form', 'url'));
    
}
    public function 
index()
    {
        
$data = array(
            
'title' => 'Welcome Profile Edit',
            
'body' => anchor(base_url().'member_area/''Member area')
        );
        
$this->load->view('_output_html'$data);
    } 

I already autoload the helper:

autoload.php

PHP Code:
$autoload['helper'] = array('html','url','form'); 

I wonder how to fix the error?


RE: undefined function anchor() - sv3tli0 - 11-04-2014

Why do you generate url inside the Controller instead inside the View?


RE: undefined function anchor() - InsiteFX - 11-04-2014

If you must do it from code you can do it like below. But sv3tli0 is correct it should be done in your html.

Code:
$out = '<li>' . anchor($categories[$i]['link'], $categories[$i]['title']) . '</li>';

$data = array(
    'body' => $out,
);

You cannot place php code inside php code like that, that's why the error.

@Narf: I meant inside an associated array, it just does not like it! But works fine this way.


RE: undefined function anchor() - Narf - 11-06-2014

(11-04-2014, 05:47 AM)InsiteFX Wrote: If you must do it from code you can do it like below. But sv3tli0 is correct it should be done in your html.

Code:
$out = '<li>' . anchor($categories[$i]['link'], $categories[$i]['title']) . '</li>';

$data = array(
    'body' => $out,
);

You cannot place php code inside php code like that, that's why the error.

Yes you can.


RE: undefined function anchor() - Hobbes - 11-06-2014

i have had the same experience as InsiteFX

going :

$data = array(
'body' => anchor($categories[$i]['link'], $categories[$i]['title'])
);

always seems to not work at all or throws an error. but putting it like InsiteFX stated works just fine


RE: undefined function anchor() - Narf - 11-06-2014

(11-06-2014, 07:32 PM)Hobbes Wrote: i have had the same experience as InsiteFX

going :

$data = array(
'body' => anchor($categories[$i]['link'], $categories[$i]['title'])
);

always seems to not work at all or throws an error. but putting it like InsiteFX stated works just fine

Nonsense.
Proof: http://3v4l.org/EfXT3

@davy_yg's issue is that he's overriding CI_Controller::__construct(), but not calling parent::__construct() in the override. The constructor is what triggers the autoloader.


RE: undefined function anchor() - Chroma - 11-08-2014

If using base_url(), I find it much easier to place the additional URL path inside the () or the function.

base_url('path/to/item')

rather than spread it out, makes the code easier too.


RE: undefined function anchor() - Chroma - 11-08-2014

(11-06-2014, 11:22 PM)Narf Wrote:
(11-06-2014, 07:32 PM)Hobbes Wrote: i have had the same experience as InsiteFX

going :

$data = array(
'body' => anchor($categories[$i]['link'], $categories[$i]['title'])
);

always seems to not work at all or throws an error. but putting it like InsiteFX stated works just fine

Nonsense.
Proof: http://3v4l.org/EfXT3

@davy_yg's issue is that he's overriding CI_Controller::__construct(), but not calling parent::__construct() in the override. The constructor is what triggers the autoloader.

In full agreement, you must uncomment the call to the controllers parent.


RE: undefined function anchor() - sv3tli0 - 11-08-2014

Small question, why you use HTML helper inside the Controller ?
This is HTML building task and should be done inside your view..


RE: undefined function anchor() - Hobbes - 11-08-2014

i was just sayin' that when I use any of the html helper functions, I half to put them in a variable before putting them into an array or else like I said, it either does not work or throws an error.