CodeIgniter Forums
Fatal error: Using $this when not in object context - 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: Fatal error: Using $this when not in object context (/thread-2560.html)



Fatal error: Using $this when not in object context - El Forum - 08-12-2007

[eluser]flumps[/eluser]
I am using codeIgniter to help with my php website, I am just wondering if somone could help me with this line of code as im at a blank am I missing to do somthing here :-s

Fatal error: Using $this when not in object context in /home/user/domains/domain/public_html/system/application/views/header.php on line 6

line 6 of the header.php file reads:

<link href="<?=$this->config->item('base_view');?>style.css" rel="stylesheet" type="text/css">

many thanks in advance.

I think its cause $this is a pre defined variable as im using php5 if anyone could help would much appreciate it.


Fatal error: Using $this when not in object context - El Forum - 08-12-2007

[eluser]coolfactor[/eluser]
Generally discouraged to do it that way. Do that in your controller, assign the value to a variable that you pass the view.

In your controller:
Code:
// get base_view variable
$data['base_view'] = $this->config->view('base_view');

// load header view
$data['header'] = $this->load->view('header', $data, TRUE);

// load main template
$this->load->view('template', $data);

In your header.php view:
Code:
<link href="<?=$base_view;?>style.css" rel="stylesheet" type="text/css">



Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]flumps[/eluser]
sorry its early here and my brain cells havent engaged yet,

do I put that code in the contollers/pages.php file?


Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]beatryder[/eluser]
[quote author="flumps" date="1187017492"]sorry its early here and my brain cells havent engaged yet,

do I put that code in the contollers/pages.php file?[/quote]

Pardon me if I seem rude, but it seems as though you don't fully understand how code igniter works?

Have you watched the videos?

Basically what you need to do is set up a controller to load a view, then pass the view the information you want it to display.

Edit: Yes, it would appear that you want to use your pages.php file.

You have not posted a great deal of information. If you are still having troubles post the entire contents of controllers/pages.php AND views/header.php so that people can get the full context of what you are trying to accomplish.


Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]flumps[/eluser]
I just new I was gunna get flamed.

no wonder why most people complain about things not working cause there so scared to ask for help without getting flamed.

1. im not a web developer.

2. my friend set it all up for me and coded the site for me.

3. hes on holiday before you flame me asking ask him.

4. hes on the very tip of scotland with no web access so no I cant text or phone him to sort it out.

5. I did ask for help from another friend be her referred me to these forums instead.

yeah sure my pages.php file looks like this:

Quote:<?php

class Pages extends Controller {

function Pages()
{
parent::Controller();
}

function index()
{
$this->load->view('index_view');
}
function about()
{
$this->load->view('welcome_message');
}
function hosting()
{
$this->load->view('hosting_view');
}
function resellers()
{
$this->load->view('resellers_view');
}
function dedicated()
{
$this->load->view('dedicated_view');
}
function contact()
{
$this->load->helper(array('form', 'url'));

$this->load->library('validation');


$rules['name'] = "required";
$rules['email'] = "required";
$rules['comment'] = "required";

$this->validation->set_rules($rules);

if ($this->validation->run() == FALSE)
{
$this->load->view('contact_view');
}
else
{
$this->load->library('email');

$this->email->from($_REQUEST['email'], $_REQUEST['name']);
$this->email->to('REMOVED');

$this->email->subject("Website Query");
$this->email->message($_REQUEST['comment']);

$this->email->send();
$this->load->view('contact_sent_view');
}
}
}
?>

I say im not a web developer but I have some understanding of how things work just new to codeigniter I was supprised my friend is using this really I didnt tell him to. it was a case of I'll code the site for you. 3 days later he sends me the site with the codigniter with it I was like oookkk.

but ive read a few artcles online and yes im aware you have to write controllers and stuff but no I havent yet watched the videos etc.

sorry for asking for help god. for future adventures i wont use it if it bothers you that much.


Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]Rick Jolly[/eluser]
We're here to help.

Since the header view is being loaded from a view, the easiest thing to do would be to create a global variable $base_view. To create global variables that are available to all your views, use $this->load->vars() like so:
Code:
class Pages extends Controller {

  function Pages()
  {
    parent::Controller();
    $data['base_view'] = $this->config->item('base_view'); // edit - fixed this: $this->config->view('base_view');
    $this->load->vars($data);
  }

  ...
}
Then in the header.php view you could write it like coolfactor suggested:
Code:
<link href="<?=$base_view;?>style.css" rel="stylesheet" type="text/css">



Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]beatryder[/eluser]
[quote author="flumps" date="1187061317"]I just new I was gunna get flamed.

no wonder why most people complain about things not working cause there so scared to ask for help without getting flamed.

1. im not a web developer.

2. my friend set it all up for me and coded the site for me.

3. hes on holiday before you flame me asking ask him.

4. hes on the very tip of scotland with no web access so no I cant text or phone him to sort it out.

5. I did ask for help from another friend be her referred me to these forums instead.

yeah sure my pages.php file looks like this:

Quote:<?php

class Pages extends Controller {

function Pages()
{
parent::Controller();
}

function index()
{
$this->load->view('index_view');
}
function about()
{
$this->load->view('welcome_message');
}
function hosting()
{
$this->load->view('hosting_view');
}
function resellers()
{
$this->load->view('resellers_view');
}
function dedicated()
{
$this->load->view('dedicated_view');
}
function contact()
{
$this->load->helper(array('form', 'url'));

$this->load->library('validation');


$rules['name'] = "required";
$rules['email'] = "required";
$rules['comment'] = "required";

$this->validation->set_rules($rules);

if ($this->validation->run() == FALSE)
{
$this->load->view('contact_view');
}
else
{
$this->load->library('email');

$this->email->from($_REQUEST['email'], $_REQUEST['name']);
$this->email->to('REMOVED');

$this->email->subject("Website Query");
$this->email->message($_REQUEST['comment']);

$this->email->send();
$this->load->view('contact_sent_view');
}
}
}
?>

I say im not a web developer but I have some understanding of how things work just new to codeigniter I was supprised my friend is using this really I didnt tell him to. it was a case of I'll code the site for you. 3 days later he sends me the site with the codigniter with it I was like oookkk.

but ive read a few artcles online and yes im aware you have to write controllers and stuff but no I havent yet watched the videos etc.

sorry for asking for help god. for future adventures i wont use it if it bothers you that much.[/quote]

My friend you are far to sensitive! Firstly, my intent was *not* to flame you:
[quote author=beatryder]Pardon me if I seem rude, but it seems as though you don’t fully understand how code igniter works?[/quote]

And I did offer you help as well!

Now that that is out of the way...

Lets have a look at what you are doing shall we?

I am assuming that header.php is loaded with a
Code:
$this->load->view('header');
in your *_view files?


Now as I said before, you need to post the contents of all the files you are using. I will work with what you have though.


You are going to have to trace through the code in your implementation to find your problems.

What you can do is something like this:
Code:
function index(){
$data['base_view'] = $this->config->item('base_view');
$this->load->view('index_view',$data);
}

then in your header.php file replace
Code:
<link href="<?=$this->config->item('base_view');?>style.css" rel="stylesheet" type="text/css">

with

Code:
<link href="<?=$base_view?>style.css" rel="stylesheet" type="text/css">

Without knowing what is in header.php and your view files there is not much else I can do to help you.


Fatal error: Using $this when not in object context - El Forum - 08-13-2007

[eluser]flumps[/eluser]
[quote author="Rick Jolly" date="1187063380"]We're here to help.

Since the header view is being loaded from a view, the easiest thing to do would be to create a global variable $base_view. To create global variables that are available to all your views, use $this->load->vars() like so:
Code:
class Pages extends Controller {

  function Pages()
  {
    parent::Controller();
    $data['base_view'] = $this->config->view('base_view');
    $this->load->vars($data);
  }

  ...
}
Then in the header.php view you could write it like coolfactor suggested:
Code:
<link href="<?=$base_view;?>style.css" rel="stylesheet" type="text/css">
[/quote]

thanks very much Smile very helpfull.

will give that a bash tomorrow as time is getting on and ive got to be up at 6am :-s.


Fatal error: Using $this when not in object context - El Forum - 08-14-2007

[eluser]flumps[/eluser]
[quote author="Rick Jolly" date="1187063380"]We're here to help.

Since the header view is being loaded from a view, the easiest thing to do would be to create a global variable $base_view. To create global variables that are available to all your views, use $this->load->vars() like so:
Code:
class Pages extends Controller {

  function Pages()
  {
    parent::Controller();
    $data['base_view'] = $this->config->view('base_view');
    $this->load->vars($data);
  }

  ...
}
Then in the header.php view you could write it like coolfactor suggested:
Code:
<link href="<?=$base_view;?>style.css" rel="stylesheet" type="text/css">
[/quote]

Ive tryed that I now get..

Fatal error: Call to undefined method CI_Config::view() in /home/username/domains/domain/public_html/system/application/controllers/pages.php on line 8

line 8 reads... $data['base_view'] = $this->config->view('base_view');


Fatal error: Using $this when not in object context - El Forum - 08-14-2007

[eluser]deviant[/eluser]
Shouldnt it be :

Code:
$this->config->item('base_view');