drwpbrv668670 2017-04-06 15:17
浏览 43
已采纳

模板中的未定义变量

I am getting the undefined variable whenever I load the controller.

Here is the index function from the controller that gets called ( 4th line ) and the same error in every other occurrence.

<?php

class Students extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('Student_model');
}

public function index() {
    $data['view_name'] = 'students/dashboard';
    $this->load->view('templates/template_student' , $data );
}

}

And the part of the template where the error comes from This is a part of the code from the template_student template

<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<?php if ($student->passport_photo == '') { ?>
<img src="<?php echo base_url(); ?>assets/images/sample.jpg" alt="">
<?php } else { ?>
<img src="<?php echo base_url(); ?><?php echo $student->passport_photo; ?>" alt="">
<?php } ?>
<?php if ($this->session->userdata('logged_in')) : ?>
<?php echo $this->session->userdata('first_name') . " " . $this->session->userdata('last_name'); ?>
<?php endif; ?>
<span class=" fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu dropdown-usermenu pull-right">
<li><a href="<?php echo base_url(); ?>students/profile">  Profile</a>
</li>
<li><a href="<?php echo base_url(); ?>students/logout"><i class="fa fa-sign-out pull-right"></i> Log Out</a>
</li>
</ul>
</li>
</ul>

What have I done wrong?

  • 写回答

2条回答 默认 最新

  • douyi1939 2017-04-06 15:51
    关注

    You have loaded a model called Student in your controller, and in your template your are accessing a $student variable. That's 2 different things.

    You should do something like this :

    class Students extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('Student_model', 'student');
        }
    
        public function index() 
        {
            $student = ... // retrieve the student from db
            $data['student'] = $student; // define the variable to be sent to the view
            $this->load->view('templates/template_student' , $data );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout