Why is my variable null?

Now I am editing a working project with some experience in PHP.

I am a little basic, but I know only a little about MVC and Code-Igniter, therefore, I will encounter some problems.

My first problem is that I am trying to populate 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 in the $ year index:

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

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

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

Does 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>
+3
source share
4

, , , CI.

:

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

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

}

, , CI ( , {menu} <?php echo $menu; ?>, , , index ' $data array)

; @praneeth, php! :

$this->load->view('overview_screen',$data)
+1

,

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

$year, $menu

+2

If you are a var_dumpvariable $menuoutside of a function / class, it will not work! Try using:

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

Can you give more code examples on where you put the Dump call, as well as the body of getMenu. This would be useful to find out what happens with $ var.

0
source

All Articles