Welcome Guest, Not a member yet? Register   Sign In
Message: Undefined property: Start::$config
#1

[eluser]Naczu[/eluser]
Hi. I am newbie. I have problem. I use the latest version 2.1.2 of codeigniter. I have a controller which is start.php like this.

Code:
<?php
class Start extends CI_Controller
{
var $base;
var $css;
function Start()
{
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
}
function hello($name = 'Guest')
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testview', $data);
}
}

and testview.php file is

Code:
<html>
<head>
<title>Web test Site</title>
<link rel="stylesheet" type="text/css" href="<?php echo
$base."/".$css;?>">
</head>
<body>
<h1>&lt;?php echo $mytitle; ?&gt; </h1>
<p class='test'> &lt;?php echo $mytext; ?&gt; </p>
&lt;/body&gt;
&lt;/html&gt;


and in config.php

Code:
$config['base_url'] = 'http://localhost/codeigniter/';
$config['css'] = 'css/style.css';

I got this error...

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Start::$config

Filename: controllers/start.php

Line Number: 8

Fatal error: Call to a member function item() on a non-object in C:\xampp\htdocs\codeigniter\application\controllers\start.php on line 8

where is the problem ???? Please help me
#2

[eluser]timmahoney[/eluser]
I think you need to add this to the top of your controller, in order to get all the stuff CodeIgniter provides

Code:
public function __construct()
{
  parent::__construct();
}
#3

[eluser]Naczu[/eluser]
omg yes it worked ... but why O_O

ok .. it worked but I couldnt get the $css and $base variables... what is wrong with the start.php controller ?

[quote author="timmahoney" date="1345572814"]I think you need to add this to the top of your controller, in order to get all the stuff CodeIgniter provides

Code:
public function __construct()
{
  parent::__construct();
}
[/quote]
#4

[eluser]timmahoney[/eluser]
The __construct method automatically runs when the Controller is loaded, and the line parent::__construct() is running the __construct in the CI_Controller class, (the parent) so it's loading all the CodeIgniter stuff like $this->config
#5

[eluser]Naczu[/eluser]
ok .. it worked but I couldnt get the $css and $base variables on the testview.php … what is wrong with the start.php controller ?
#6

[eluser]timmahoney[/eluser]
The "Start" method you have in there is not getting run automatically. Try this:
Code:
&lt;?php
class Start extends CI_Controller
{
var $base;
var $css;
function __construct()
{
  parent::__construct();
  $this->base = $this->config->item('base_url');
  $this->css = $this->config->item('css');
}
function hello($name = 'Guest')
{
  $data['css'] = $this->css;
  $data['base'] = $this->base;
  $data['mytitle'] = 'Welcome to this site';
  $data['mytext'] = "Hello, $name, now we're getting dynamic!";
  $this->load->view('testview', $data);
}
}
#7

[eluser]Naczu[/eluser]
aww thank you so much ... Smile you solved my problem .
#8

[eluser]timmahoney[/eluser]
Glad I could help!




Theme © iAndrew 2016 - Forum software by © MyBB