开发者

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>


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


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)


If you var_dump the $menu variable outside the function/class, it won't work! Try using:

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


Can you give more code example about in which place you put the Dump call, and also about getMenu body. This would be useful in order to know what is happen with the $years var

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜