EllisLab text mark
Advanced Search
1 of 2
1
   
Web blog in 20 min
Posted: 01 August 2011 02:40 PM   [ Ignore ]
Joined: 2011-08-01
17 posts

Okay. I am a new to codigniter and I am trying to get ci 2.2 to work with the outdated “blog in 20 min tutorial”.

I am getting the following error when I am trying to use the anchor function.

PHP Fatal error:  Call to undefined function anchor() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\ci\\application\\views\\blog_view.php on line 13

Any help is greatly appreciated!

Below are my controller and view scripts:

<?php

class Blog extends CI_Controller {
    
    
function _construct()
    
{
        parent
::_construct();
        
        
$this->load->helper('url');
        
$this->load->helper('form');
    
}
    
    
function index()
    
{
        $data[
'title'"My Blog Title";
        
$data['heading'"My Blog Heading";
        
$data['query'$this->db->get('entries');
        
        
$this->load->view('blog_view'$data);
    
}
}

?> 
<html>
    <
head>
        <
title><?=$title?></title>
    </
head>
    <
body>
        <
h1><?=$heading?></h1>
        
        
<?php foreach($query->result() as $row): ?>
        
        
<h3><?=$row->title?></h3>
        <
p><?=$row->body?></p>
        
        <
p><?=anchor('blog/comments''Comments');?></p>
        
        <
hr>
        
        
<?php endforeach; ?>
        
        
    
</body>
</
html
 
Posted: 01 August 2011 03:02 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2006-08-26
660 posts

Replace the two _construct with __construct

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

 
Posted: 01 August 2011 03:55 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-08-01
17 posts

That fixed my current issue. Thank you.

 
Posted: 14 September 2011 04:36 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2011-09-14
8 posts

hi i also have the same problem. it says Fatal error: Call to undefined function anchor() in C:\xampp\htdocs\CodeIgniter_2.0.3\application\views\blog_view.php on line 15

i am using the latest version 2.0.3 codeigniter.

 Signature 

Light Peace Love

 
Posted: 14 September 2011 02:27 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2006-08-26
660 posts

@bamboo
This is not the same problem, did You load the URL helper before calling anchor()?

If it won’t work, open a new thread and post the code (bonus points for having reduced the problem, will mean take out all unnecessary stuff)

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

 
Posted: 27 July 2012 09:38 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-07-27
2 posts

I am having a similar or same issue.
I have set up a controller and a view and I am trying to use a helper function. I am getting a message that:


[27-Jul-2012 21:30:20] PHP Fatal error:  Call to undefined function anchor() in D:\wamp\www\proj\system\core\Loader.php(829) : eval()‘d code on line 2

class Perspective extends CI_Controller{
 
 
function index()
 
{
  
  $this
->load->helper('url');
  
  
$this->load->view('header');
  
$this->load->view('welcome');
  
$this->load->view('footer');

 
}//end Index

}

//////VIEW

<?anchor('http://www.msn.com''Click Here');?> 
 
Posted: 28 July 2012 12:28 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2006-08-26
660 posts

Maybe You don’t pass via the method index ?

Try to put the helper load call in the constructor of the controller, or directly in the view.

If You use the URL helper sidewide, You can also autoload it.

 Signature 

Die Wirklichkeit ist das, was übrig bleibt, wenn man aufgehört hat, daran zu glauben.

 
Posted: 28 July 2012 04:44 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-07-27
2 posts

Thanks its working now LOL

 
Posted: 19 August 2012 01:12 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-08-19
6 posts

Hello!  I am also just getting started with the blog tutorial, experiencing a different problem.  The anchor() function is supposed to return the URL of

“localhost/codeIgniterTest/index.php/blog/comments/1”

And indeed when I put that URL into the address bar, the “testing ....” message comes up successfully.  Instead it returns:

“http://localhost/codeIgniterTest/localhost/codeIgniterTest/index.php/blog/comments/1”

Which results in a 404 error.  I’m not sure why it’s doing this.  I’m trying to backtrack it through all the source files (i.e., figure out if it’s something in site_url() or base_url(), but I’m pretty hesitant to mess with any of that stuff.  Any help is much appreciated!  Here is the relevant code snippet:

<p><?=anchor('blog/comments/'.$row->id'Comments');?></p

UPDATE

Holy balls.  Ok so I did some digging into the site_url() function.  Here it is:

function site_url($uri '')
 
{
  
if ($uri == '')
  
{
   
return $this->slash_item('base_url').$this->item('index_page');
  
}

  
if ($this->item('enable_query_strings') == FALSE)
  
{
   $suffix 
= ($this->item('url_suffix') == FALSE) ? '' $this->item('url_suffix');
   return 
$this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
  
}
  
else
  
{
   
return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
  
}
 } 

What I found is that

a) enable_query_strings is apparently set to FALSE.  Don’t know how or why that is, it just is

b) if I remove “$this->slash_item(‘base_url’).” from that 2nd of the above 3 return statements, the link works properly.  Adding the base_url is, I’m guessing, redundant, since apparently it’s already included in the URI for ‘index_page’.  All that is speculation on my part.

I suspect this may have to do with my own settings, about which I know very little, having just installed this yesterday.  I am using version 2.1.2.

 Signature 

Wearing stripes with plaid
still can’t find the ‘any’ key

 
Posted: 19 August 2012 01:48 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts

@patricksayshi

You will need to look at your config.php file in /config/config.php.

What is the value of $config[‘base_url’]?

It should be something like this:

$config['base_url''http://localhost/mysite/';  // the closing forward slash is important 

Please post what you are using in $config[‘base_url’]

[EDIT]

a) enable_query_strings is apparently set to FALSE.  Don’t know how or why that is, it just is

CI does not encourage the user of GET and URL variables (?var=xxx), instead, use the uri segment approach where variables are passed as separate segments.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 19 August 2012 01:53 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-08-19
6 posts

Wow, thanks for the quick reply.  Even though I’ve got this working I’d definitely like to follow this through to make sure I’m not doing something that’s going to mess me up in the long run.  So again, thanks.  Here is my base_url:

$config['base_url''localhost/codeIgniterTest'

UPDATES:
I have just added the closing forward slash, then restored the ‘base_url’ that I had removed above, but I’m still getting the same error.

 

 

I haven’t done anything to my htaccess file, so here are its entire contents: 

“Deny from all”

 

 

 Signature 

Wearing stripes with plaid
still can’t find the ‘any’ key

 
Posted: 19 August 2012 02:06 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
patricksayshi - 19 August 2012 01:53 PM

Wow, thanks for the quick reply.  Even though I’ve got this working I’d definitely like to follow this through to make sure I’m not doing something that’s going to mess me up in the long run.  So again, thanks.  Here is my base_url:

$config['base_url''localhost/codeIgniterTest'

I have just added the closing forward slash, then restored the ‘base_url’ that I had removed above, but I’m still getting the same error.

I haven’t done anything to my htaccess file, so here are its entire contents: 

“Deny from all”

 

Ok, your .htaccess is fine for what you are doing.

I use a virtual host on my local server so I have a fully qualified domain name (not localhost).
Try putting the full http:// on yours:

$config['base_url''http://localhost/codeIgniterTest/'

[EDIT] it needs the trailing slash, duh!

FYI, I note that you are using capital letters in your folder name. If your OS is case sensitive, that is important, but not related to the configuration discussion.  I try to stay with all lower case folder names to avoid misspelling problems.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 19 August 2012 02:10 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2012-08-19
6 posts

Thanks again! 

I have now tried adding http:// to the beginning of the base_url and it’s still not doing the trick.  The only thing that’s working for me is still removing the base_url from the site_url output ...  Hmmm. ..

I’ll keep that in mind about capitalization in the folder titles!  I’ve just been learning programming for the past few months, and I started with javascript so my instinct seems to be to use js variable naming conventions everywhere

 Signature 

Wearing stripes with plaid
still can’t find the ‘any’ key

 
Posted: 19 August 2012 02:18 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts

My bad, the trailing slash was missing on my previous post (fixed now).
Did you use the trailing slash?

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 19 August 2012 02:20 PM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2012-08-19
6 posts

Did you use the trailing slash?

Yup.  It’s now:

$config['base_url''http://localhost/codeIgniterTest/'

and still the same thing.

 Signature 

Wearing stripes with plaid
still can’t find the ‘any’ key

 
Posted: 19 August 2012 02:33 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts

I note that in your first post, you have edited a “core” file.  This is not a good idea, as it can break other things.  Did you restore your changes before testing my suggested base_url setting in config.php?

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
1 of 2
1