CodeIgniter Forums
Problem with function Blog() in first tutorial. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with function Blog() in first tutorial. (/thread-54934.html)



Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]SimonLove2009[/eluser]
Hi

I've just completed the first tutorial, but I've run into a problem right at the end—In the tutorial, he recommends inserting a function Blog() to make sure the Blog class is always called. However, this by itself causes a php error. The error is rectified in the tutorial by inserting - parent::Controller();

When I put parent::Controller(); into my code, the view page just appeared to be empty. Why is this? I copied the code very carefully indeed. I even tried changing parent::Controller(); to parent:: CI_Controller(); as per in the class at the top my blog.php—Still no joy. What am I doing wrong?

Kind regards

Simon


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]NotDior[/eluser]
Would it be possible to post your code. it sounds like it's a rather easy fix. This is probably what the start of your controller class needs to look like:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {

function __construct() {
  parent::__construct();
  $this->load->model("Blogmodel");
}

public function index()
{
  $xProducts = $this->Blogmodel->getPosts();
        }

Realize that tutorial is really old (assuming it's the video) and a lot of things have changed.



Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]SimonLove2009[/eluser]
Thanks for responding. Here is the code that doesn't seem to be working:—

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {
    
    function Blog()
    {
      parent::Controller();
    }
  
  

function index()
{
  $data['title'] = "My Blog Title";
  $data['heading'] = "My Blog Heading";
  $data['todo'] = array('clean house', 'eat lunch', 'call Mom');
  
  $this->load->view('blog_view', $data);
  
}

}

?>

I tried putting CI_Controller as the parent, but this didn't work either. Any help much appreciated. BTW do you know of any more up to date tutorials I can learn from?

Kind regards

Simon Lovena


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]keevitaja[/eluser]
remove

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



Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]SimonLove2009[/eluser]
Hi

Thanks for that. I already knew that removing this code would make it work again. However, that still doesn't explain why it will not work.

You said that this video tutorial is very old, which it is. Do you know where the best CodeIgniter tutorials which deal with the recent release of CI are on the internet?

Thanks for your help.

Simon


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]keevitaja[/eluser]
this video tutorial is written for older version of CI and PHP.
correct method would be:

Code:
function __construct() {
  parent::__construct()
}



Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]SimonLove2009[/eluser]
Thanks. That worked a treat apart from you forgot the semi colon after construct();

So presumably instead of always including the old code that doesn't work, I should always include

function __construct() {
parent::__construct();
}

In every document?

Thanks once again for your help.

Simon


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]keevitaja[/eluser]
no, only if you want to use __construct() method.

maybe you should first learn PHP basics and then start with a framework... anyway this CI tutorial is more uptodate + there are many available on youtube


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]SimonLove2009[/eluser]
Hi

I think you're right—I'll get to grips with some basic php first.

thanks for all your help.

Simon


Problem with function Blog() in first tutorial. - El Forum - 10-02-2012

[eluser]keevitaja[/eluser]
for OOP try this tutorial