Welcome Guest, Not a member yet? Register   Sign In
20-min-blog-tutorial - "Call to undefined function anchor() in"
#1

[eluser]muhax[/eluser]
Hello everyone.
I am doing the 20-min-blog tutorial and I am stuck on url helper anchor function.
When I load the page with this
Code:
<?php foreach ($query->result() as $row):?>
<h3>&lt;?= $row->title ?&gt;</h3>
<p>&lt;?= $row->body ?&gt;</p>
<p>&lt;?= anchor('blog/comments', 'Comments'); ?&gt;</p>
<hr>
&lt;?php endforeach;?&gt;

it breaks at anchor. The html source is
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Blog Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>My Blog Heading</h1>
<h3>My first entry</h3>
<p>haha aha a ha</p>
<p>

I go to php error logs and it sais.


PHP Fatal error: Call to undefined function anchor() in C:\Users\****\ZendServerCE\Apache2\htdocs\codeIgniter\Invent\system\application\views\blog_view.php on line 12

Im using ZendServerCE.

Anyone know what might be the problem here?
#2

[eluser]Shaun Andrews[/eluser]
Are you loading the URL helper: http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]muhax[/eluser]
[quote author="Shaun Andrews" date="1286996571"]Are you loading the URL helper: http://ellislab.com/codeigniter/user-gui...elper.html[/quote]

Yes, my blog.php looks like:

&lt;?php
class Blog extends Controller{
function index(){

function Blog(){
parent::Controller();

$this->load->helper('url');
$this->load->helper('form');
}

$data['title'] = "My Blog Title";
$data['heading'] = "My Blog Heading";
$data['query'] = $this->db->get('entries');

$this->load->view('blog_view', $data);
}
}
?&gt;
#4

[eluser]Bart Mebane[/eluser]
You need to move your constructor outside of the index method:

Code:
&lt;?php
class Blog extends Controller{

  function Blog(){
      parent::Controller();
        
      $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);
  }
}
#5

[eluser]Shaun Andrews[/eluser]
Your constructor shouldnt be inside your index function. Try this:



&lt;?php
class Blog extends Controller{
  function index(){
      $this->load->helper(‘url’);
      $this->load->helper(‘form’);

      $data[‘title’] = “My Blog Title”;
      $data[‘heading’] = “My Blog Heading”;
      $data[‘query’] = $this->db->get(‘entries’);
      
      $this->load->view(‘blog_view’, $data);
  }
}
?&gt;

You may want to consider adding the url and form helpers to your autoloader: http://ellislab.com/codeigniter/user-gui...oader.html
#6

[eluser]muhax[/eluser]
[quote author="Shaun Andrews" date="1286997558"]Your constructor shouldnt be inside your index function. Try this:



&lt;?php
class Blog extends Controller{
  function index(){
      $this->load->helper(‘url’);
      $this->load->helper(‘form’);

      $data[‘title’] = “My Blog Title”;
      $data[‘heading’] = “My Blog Heading”;
      $data[‘query’] = $this->db->get(‘entries’);
      
      $this->load->view(‘blog_view’, $data);
  }
}
?&gt;

You may want to consider adding the url and form helpers to your autoloader: http://ellislab.com/codeigniter/user-gui...oader.html[/quote]

Thank you guys, it works now. That video tutorial is misleading, they should review it.
#7

[eluser]Shaun Andrews[/eluser]
Glad we could help. I remember working off of those video tutorials almost a year ago, and then they were outdated. I'm sure by now (and with CI2 looming) they're way out of date. They're a good start. There's some other sites that have more recent video (and text) tutorials.




Theme © iAndrew 2016 - Forum software by © MyBB