未定义的变量/尝试获取非对象Codeigniter / PHP的属性

时间:2011-11-23 10:47:12

标签: php codeigniter variables object

免责声明:我是网络开发的新手。

我似乎无法弄清楚在使用JavaScript在单击列表项时显示信息的情况下出现了什么问题。我已经在我的控制器中声明了我的变量,但是我得到了一个未定义的变量错误以及非对象错误的获取属性。有什么想法吗?非常感谢你的帮助!

以下是错误消息:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: event

Filename: user/planner_view.php

Line Number: 27

A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: user/planner_view.php

Line Number: 27

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: event

Filename: user/planner_view.php

Line Number: 29

A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: user/planner_view.php

Line Number: 29

Status
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: status_options

Filename: user/planner_view.php

Line Number: 32

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: event

Filename: user/planner_view.php

Line Number: 32

A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: user/planner_view.php

Line Number: 32

A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331



Title

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: event

Filename: user/planner_view.php

Line Number: 37

A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: user/planner_view.php

Line Number: 37

这是控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Planner extends Common_Auth_Controller {

    private $end_user;

    public function __construct()
    {
        parent::__construct();

        $this->end_user = $this->ion_auth->user()->row();
        $data['end_user'] = $this->end_user;
        $this->load->vars($data);

        $this->load->model('events_model', 'events');
    }

    public function populate_events()
    {
        $this->load->model('events_model', 'events');
        $this->events->populate_events($this->end_user->id);
    }

    public function index()
    {

        $config['start_day'] = 'sunday';

        $config['month_type'] = 'long';

        $config['day_type'] = 'long';

        $config['show_next_prev'] = TRUE;

        $config['next_prev_url'] = base_url('user/planner/index/');

        $config['template'] = '

            {table_open}<table class="planner">{/table_open}

            {heading_row_start}<tr>{/heading_row_start}

            {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
            {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
            {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}

            {heading_row_end}</tr>{/heading_row_end}

            {week_row_start}<tr>{/week_row_start}
            {week_day_cell}<th class="day_header">{week_day}</td>{/week_day_cell}
            {week_row_end}</tr>{/week_row_end}

            {cal_row_start}<tr>{/cal_row_start}
            {cal_cell_start}<td>{/cal_cell_start}

            {cal_cell_content}<span class="day_listing">{day}</span><ul class="event_list">{content}</ul>{/cal_cell_content}
            {cal_cell_content_today}<div class="today"><span class="day_listing">{day}</span><ul class="event_list">{content}</ul>{/cal_cell_content_today}

            {cal_cell_no_content}<span class="day_listing">{day}</span>&nbsp;{/cal_cell_no_content}
            {cal_cell_no_content_today}<div class="today"><span class="day_listing">{day}</span></div>{/cal_cell_no_content_today}

            {cal_cell_blank}&nbsp;{/cal_cell_blank}

            {cal_cell_end}</td>{/cal_cell_end}
            {cal_row_end}</tr>{/cal_row_end}

            {table_close}</table>{/table_close}

        ';

        $this->load->library('calendar', $config);

        $title['title'] = 'Planner';

        $data = $this->events->get_events_for_calendar($this->end_user->id, $this->uri->segment(4), $this->uri->segment(5));

        $calendar['calendar'] = $this->calendar->generate($this->uri->segment(4), $this->uri->segment(5), $data);

        $this->load->view('public/head_view', $title);
        $this->load->view('user/header_view');
        $this->load->view('user/planner_view', $calendar);
        $this->load->view('user/footer_view');
    }

    public function update($event_id)
    {   

        if($_POST)
        {
            $title = $this->input->post('title');
            $status = $this->input->post('status');
            $content = $this->input->post('content');

            $data = array(
                "id" => $event_id,
                "title" => $title,
                "status" => $status,
                "content" => $content
            );

            if ($this->events->update($data))
            {
                echo "YAY";
            }
            else
            {
                echo "CRAP";
            }
        }
        else
        {
            $data = array();

            $data['event'] = $this->events->get($event_id);

            $data['status_options'] = array(
                NULL => "Select",
                "C"  => "Completed",
                "L"  => "Completed Late",
                "N"  => "Not Completed"
            );

            echo $this->load->view("user/planner_view", $data, TRUE);
        }
    }
}

以下是观点:

<?php echo form_open('user/planner/update/'.$event->id, 'id="updateForm"'); ?>

            <h3><?php echo $event->type ?></h3>

            <label for="status">Status</label>
            <?php echo form_dropdown('status', $status_options, $event->status ) ?>
            <br/>
            <br/>

            <label for="title">Title</label>
            <input type="text" id="title" name="title" value="<?php echo $event->title ?>" />
            <br/>
            <br/>

            <label for="content">Content</label>
            <textarea id="content" name="content"><?php echo $event->content ?></textarea>
            <br/>
            <br/>

            <p><?php echo form_submit('submit','Save') ?>

            <?php echo form_close(); ?>

2 个答案:

答案 0 :(得分:2)

我假设这条线$data['event'] = $this->events->get($event_id);是导致你麻烦的那条线,这对你来说是否合适?在将$data传递给视图之前,您是否可以通过添加以下内容来检查$this->events->get($event_id);方法是否将对象返回到$data['event]`数组:

$data['event'] = $this->events->get($event_id);    
var_dump($data);
die();

如果$data['event']可能为空,您应该先使用isset($event);在视图中检查它,然后再尝试在页面中使用它。

答案 1 :(得分:1)

我的猜测:您的错误来自控制器索引功能结束时的这一行

$this->load->view('user/planner_view', $calendar);

据我所知,$calendar不包含eventstatus_options

的值