ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Web blog in 20 min

August 01, 2011 2:40pm

Subscribe [7]
  • #1 / Aug 01, 2011 2:40pm

    selftaught

    10 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?><br />
            <br />
            <?=anchor('blog/comments', 'Comments');?><br />
            <br />
            <hr><br />
            <br />
            <?php endforeach; ?><br />
            <br />
            <br />
        </body><br />
    </html>

  • #2 / Aug 01, 2011 3:02pm

    tomcode

    654 posts

    Replace the two _construct with __construct

  • #3 / Aug 01, 2011 3:55pm

    selftaught

    10 posts

    That fixed my current issue. Thank you.

  • #4 / Sep 14, 2011 4:36am

    bamboo

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

  • #5 / Sep 14, 2011 2:27pm

    tomcode

    654 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)

  • #6 / Jul 27, 2012 9:38pm

    webface

    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');?>
  • #7 / Jul 28, 2012 12:28am

    tomcode

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

  • #8 / Jul 28, 2012 4:44am

    webface

    2 posts

    Thanks its working now :lol:

  • #9 / Aug 19, 2012 1:12pm

    patricksayshi

    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:

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

    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.

  • #10 / Aug 19, 2012 1:48pm

    TWP Marketing

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

  • #11 / Aug 19, 2012 1:53pm

    patricksayshi

    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”

     

     

  • #12 / Aug 19, 2012 2:06pm

    TWP Marketing

    596 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';

    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.

  • #13 / Aug 19, 2012 2:10pm

    patricksayshi

    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

  • #14 / Aug 19, 2012 2:18pm

    TWP Marketing

    596 posts

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

  • #15 / Aug 19, 2012 2:20pm

    patricksayshi

    6 posts

    Did you use the trailing slash?

    Yup.  It’s now:

    $config['base_url'] = 'http://localhost/codeIgniterTest/';

    and still the same thing.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases