Welcome Guest, Not a member yet? Register   Sign In
please help ! - Create Library Load Data Fail
#1

[eluser]Andychan[/eluser]
application/libraries/LoadData.php

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

class LoadData {

    function __construct()
    {    
        $a = "test";    
    }
}

?>

application/controllers/about.php

Code:
<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {

    public function index(){    
        $this->load->library('LoadData');
        $data['a'] = $a;
        
        // layout part
        $this->load->view('header',$data);
        $this->load->view('about_view');
        $this->load->view('footer');
    }
}

/* End of file about.php */

application/views/about_view.php
Code:
<div class="right">
        
            About Me :) <bt />
            &lt;?=$a;?&gt;
            
        </div>
    
    <div class="clear"></div>&lt;!-- end clear --&gt;
    
    </div>&lt;!-- end main --&gt;

Show Error Message
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: a

Filename: controllers/about.php

Line Number: 13
#2

[eluser]osci[/eluser]
As the message says, a is undefined. Read about php scope variables in php manual. Search forum. A 1 minute search and I found this thread.
#3

[eluser]Andychan[/eluser]
I try it, but.....

Quote:Fatal error: Call to undefined method About::loaddata()
#4

[eluser]osci[/eluser]
You need to read a little about variable scopes

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

class LoadData {
    public $a='test';

    function __construct()
    {    
        //$a = "test";    
    }
}

// or
class LoadData {
    public $a;

    function __construct()
    {    
        $this->a = "test";    
    }
}

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

class About extends CI_Controller {

    public function index(){    
        $this->load->library('Loaddata');
        $data['a'] = $this->loaddata->a;
        
        // layout part
        $this->load->view('header',$data);
        $this->load->view('about_view');
        $this->load->view('footer');
    }
}
#5

[eluser]Andychan[/eluser]
oh ! thanks for your great help !
It works !
#6

[eluser]Andychan[/eluser]
May I ask one more question.

How can I save array data and get it?
I checked it on http://php.net/manual/en/language.variables.scope.php

It is not a clearly understand.
#7

[eluser]osci[/eluser]
I don't understand what you mean by save array data.
#8

[eluser]Andychan[/eluser]
I settled it,thanks !

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

class LoadData {
    
    public $arr = array(
                "a" => "a",
                "b" => "b",
    );

    function __construct()
    {    
        //
    }
}

?&gt;
#9

[eluser]Frank Wong[/eluser]
@Andychan, just to clarify for the future, you were asking how to set or populate an array. Not save array data.




Theme © iAndrew 2016 - Forum software by © MyBB