Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my variable null?

I'm currently editing a working project with some experience on PHP.

I know a bit about the basics but I know only a little about MVC and Code-Igniter so ofcourse I will be facing some problems.

My first problem is that i'm trying to fill a drop down list from a controller.

The view is called "overview_screen.php" and the controller is called "overview.php".

In the controller I have a function:

private function getYears()
{
    return array('Test1', 'Test2', 'Test3', 'Test4');
}

which I set to $years in the index:

function index()
{
    $years = $this->getYears();             
    $menu = $this->getMenu();       
}           

when I do a var_dump on the $menu it shows the menu as it should be shown but when I do the var_dump on $years it says:

PHP Error was encountered   
Severity: Notice    
Message: Undefined variable: years    
Filename: views/overview_screen.php    
Line Number: 92

Anyone know why this is happening / not working ?

[edit]

Added information:

<?php
print_r(CI_session::userdata('docent_id'));

if (!$this->session->userdata('docent_id')) {
    header ('Location: /bpv-registratie/welcome.html');
}
?>

<html>
<head>
<title>Registratie Systeem</title>

</head>
<body>

<div id="topmenu">
    <a href="/registratie/" class="button">Start</a>
    <a href="/registratie/welcome/logout.html" class="button">Log uit</a>       
</div>

<div id="topmenuright">
<?php var_dump($years); ?>
    <select>        
        <?php foreach ($years as $row):?>
            <option><?=$row?></option>
        <?php endforeach;?>
    </select>
</div>

<div id="menu">
    {menu}
</div>

<div id="content">
    {content}
</div>

</body>
</html>
like image 816
Theun Arbeider Avatar asked May 17 '26 08:05

Theun Arbeider


2 Answers

You need to send variables to view file as below

function index()
{
    $data['years'] = $this->getYears();             
    $data['menu'] = $this->getMenu();       
    $this->load->view('views/overview_screen.php',$data);
}

now you can use $year, $menu as variables in view file

like image 183
praneeth Avatar answered May 19 '26 23:05

praneeth


Given the output, your view is likely to be passing through the parser library of CI.

Code should be like this:

function index()
{
    $this->load->library('parser');
    $data['years'] = $this->getYears();             
    $data['menu'] = $this->getMenu(); 

    $this->parser->parse('overview_screen',$data);

}

I say this because I see the use of brackets inside the view file, which is how CI uses its own parsing engine (thus {menu} is treated like <?php echo $menu; ?>, which, in turn, refers to the index 'menu' you set in $data array)

You call it a view regularly; it's like @praneeth code, but without using the php extension! so:

$this->load->view('overview_screen',$data)
like image 33
Damien Pirsy Avatar answered May 19 '26 22:05

Damien Pirsy