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条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮