CodeIgniter Forums
Undefined index - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Undefined index (/thread-1845.html)



Undefined index - El Forum - 06-30-2007

[eluser]matt2012[/eluser]
I want to do something like this in the controller (see below),

but if I have nothing stored in $data['message'] I get Undefined index: message

is this bad code or should I just disable notice warnings?

Code:
activation
{

            $data['message'] = 'Activation complete. You can now start your plan.';
        $this->index($data);
}

index
{

if($data['message']) {
        if($this->input->post('login') == ''){
        $data['message'] = 'please login.';
         }
         else {
         $data['message'] = ''; }
     }

}


The second issue is how can I store

$data['message'] = 'something';

in the default::controller variables registered there dont seem to be available to other controllers.

Sorry for asking two questions..


Undefined index - El Forum - 06-30-2007

[eluser]Rick Jolly[/eluser]
Question #1:

Use
Code:
if (isset($data['message']]))
{
   // ...
}

Question #2:

Use a class variable for $data to access it in more than one method of the same controller:
Code:
class Something extends Controller
{
  var $data = array();

  activation
  {
    $this->data['message'] = 'Activation complete. You can now start your plan.';
  }

  index
  {
    if (isset($this->data['message']))
    {
       // do something
    }
  }
}



Undefined index - El Forum - 06-30-2007

[eluser]Phil Sturgeon[/eluser]
These are both useful points and are indeed things you should do. However the reason for the error message (which was not addressed above) is that you are defining an index in an array that has not been declared.

In C++ or many other languages the debugger would shout names until you bleed, luckily PHP only see's this as a "oh by the way" type warning and is not normally picked up. However CI shows all warnings, therefore ALWAYS define variable types before modifying them.

For example:

Code:
// GOOD
$foo = array();
$foo['something'] = "val";

// BAD
$bar['something'] = "stuff";

Using class variables to do this is one method of doing it, but only required if you are sharing the variable through multiple functions.


Undefined index - El Forum - 06-30-2007

[eluser]Rick Jolly[/eluser]
[quote author="thepyromaniac" date="1183260490"]...the reason for the error message (which was not addressed above) is that you are defining an index in an array that has not been declared.[/quote]
Actually, in php an array doesn't need to be declared.
Code:
// This works
$foo = array();
$foo['something'] = "val";

// This is OK too
$bar['something'] = "stuff";



Undefined index - El Forum - 07-01-2007

[eluser]matt2012[/eluser]
Still having problems with this - thanks for your help but im still not getting it,

here's the problem again,


Code:
class Welcome extends Controller {

var $data = array();

    
    function  Welcome()
     {

    /////want this for all functions in controller - actually maybe all functions in all controllers?/////

    $data['js'] = array('init');
    $data['css'] = array('style');
    $data['page'] = 'home';
    $data['topform'] = 'login';
    $data['title'] = 'mysite| home';
    $data['keywords'] = 'great site';
    $data['description'] = 'this site is a ...';    
    $data['message'] = '';
  
        parent::Controller();    
        }

        function index()
        {
        //leave default settings set in constructor
        $this->load->view('container', $data);

        }

        function foo()
        {
        //overwrite title variable pass on rest unchanged
        $data['title'] = 'easy2health | foo';
        $this->load->view('container', $data);
        }

hope you can help its a real stumbling block that I think will help me understand classes and how they work a lot better!


Undefined index - El Forum - 07-01-2007

[eluser]matt2012[/eluser]
Ok here's how ive solved this one

created a helper

loaded the helper in the constructor

in the helper I created a function called template($page) where ive assigned lots of $data['foo'] = 'bar';

and then assigned $data = template('home') in the functions within the controller

this way I can keep the template separate from the controller functions - I guess this is not miles away from
Yats but I found yats a bit more than I needed.


Undefined index - El Forum - 07-01-2007

[eluser]Rick Jolly[/eluser]
I'm happy you got something working Matt. The helper is a good idea, but I think you missed how to reference class variables. Remember that when referencing class variables you need to use "$this". You might want to take another look at my answer to question #2. When you used "$data" instead of "$this->data" within your methods, you were actually creating and using a variable local to the method (and not available within other methods).


Undefined index - El Forum - 07-01-2007

[eluser]marcoss[/eluser]
[quote author="Rick Jolly" date="1183242120"]Question #1:
Question #2:

Use a class variable for $data to access it in more than one method of the same controller:
[/quote]

That will work if you want to use the data across the controller, but if yo have to pass a large amount of data like i do to the views to build the UI ¬°dynamically using a common data structure, you can use a Model to store the structure and logic of the data you'll be passing to the views, just a simple example:
Code:
// Data Model

class DataModel extends Model {

      function DataModel() {
        parent::Model();
    }
    
    private $foo = array();
        
    function set_foo($key='',$val=''){
        $this->foo[$key] = $val;
    }
    
    function get_foo(){
        return $this->foo;
    }    
}

    
// Controller using the Data Model

class Article extends Controller {

    function Article(){
        parent::Controller();
        $this->load->model('DataModel');
        
        $this->DataModel->set_foo('global_option', 'foo');
        $this->DataModel->set_foo('another_global', 'bar');
    }
    
    function index(){
        $this->DataModel->set_foo('title', 'Hello World');
        $this->DataModel->set_foo('color', '#003322');
        $this->DataModel->set_foo('sidebar', 'extended');

        $data = $this->DataModel->get_foo();

        $this->load->view('article', $data);
    }
}

That is a simple example but you can think it like and active record approach and even add some logic to it so it will auto-populate fields left blank with some default values, that's up to you.