Welcome Guest, Not a member yet? Register   Sign In
PHP Error = Undefined property: User::$view_data
#1

[eluser]MikeL85[/eluser]
Hi. I'm a neewbie to MVC Framworks, CodeIgnitor, OOP - in fact I'd never even heard the term "PHP Framework" until a couple of days ago.

Anywayyy.....

I have tried to follow a simple tutorial but have fallen at the first hurdle. I have tried to follow this guy as close I could. I got told by a friend to replace "Controller" with "CI_Controller" but that hasn't stopped the following errors from appearing;

A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$view_data
Filename: controllers/user.php
Line Number: 19

And:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: base_url
Filename: views/view_register.php
Line Number: 10

Here is my user.php code:

Code:
<?php

class User extends CI_Controller{
    
    function User()
    {
        parent::CI_Controller();
        $this->view_data['base_url'] = base_url();
    }
    
    
    function index()
    {
        $this->register();  
    }
    
    function register()
    {
        $this->load->view('view_register', $this->view_data);
    }
    
}

?>

and also my view_register.php code:

Code:
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1>User Registration</h1>

&lt;?php

echo form_open($base_url . 'user/register');

$username = array(
    
    'name'  =>  'reg_username',
    'id'  =>  'reg_username',
    'value'  =>  ''
    
    );

?&gt;

<ul>
    <li>
        <label>Username</label>
        <div>
            &lt;?php echo form_input($username); ?&gt;
        </div>
    </li>
    
    <li>
        <div>
            &lt;?php echo form_submit(array('name' => 'register'), 'Register'); ?&gt;
        </div>
    </li>
    
</ul>


&lt;?php echo form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;


It's so off-putting when things like this happen. Please help to stop me from giving up and chucking all this codeignitor stuff in the trash!
#2

[eluser]Glazz[/eluser]
hmm try this:
Code:
class User extends CI_Controller{
    
    var $view_data = array();

    function __construct()
    {
        parent::CI_Controller();
        $this->view_data['base_url'] = base_url();
    }
    
    
    function index()
    {
        $this->register();  
    }
    
    function register()
    {
        $this->load->view('view_register', $this->view_data);
    }
    
}

See if it works..
#3

[eluser]MikeL85[/eluser]
Thanks. That solution partly works. If I leave in the __construct function (or the User() function for that matter) I get a blank page on loading. If I leave those functions out I still get the second error;

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: base_url
Filename: views/view_register.php
Line Number: 10

But declaring $view_data as an array got rid of one error so well done there...
#4

[eluser]Glazz[/eluser]
The construct function is called automatically if you don't use the construct the base_url will not be set so you can use on your view, thats why you are getting the undefined error..
#5

[eluser]CroNiX[/eluser]
Why do you need to pass base_url(), which is globally available? Just use it directly in your view.

You are mixing php4 and php5 syntax.

In php4, you name a class method after the name of the class to create the constructor. In php5, you just use __construct(). So, create the __construct() and move all of your code from the user() method into that, but don't use both together.

PHP5 properties don't get declared using "var". Declare them by their visibility, like "public", "private", "protected", etc.

public $view_data; instead of var $view_data;
#6

[eluser]CroNiX[/eluser]
Code:
function __construct()
{
  parent::CI_Controller();
  $this->view_data['base_url'] = base_url();
}

Should also be:

Code:
function __construct()
{
  parent::__construct();  //Parent is the construct
  $this->view_data['base_url'] = base_url();
}




Theme © iAndrew 2016 - Forum software by © MyBB