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

时间:2022-04-03 15:42:22

Disclaimer: I'm new to web development.

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

I can't seem to figure out what is wrong with this situation in where I'm using JavaScript to display information when a list item is clicked. I've declared my variable within my controller, yet I'm getting an Undefined Variable Error along with a Get Property of Non-Object Error. Any thoughts? Thank you so much for your help!

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

Here are the error messages:

以下是错误消息:

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

Here is the Controller:

这是控制器:

<?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);
        }
    }
}

Here is the view:

以下是观点:

<?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 个解决方案

#1


2  

I'm making an assumption that this line $data['event'] = $this->events->get($event_id); is the one causing you trouble, does that seem right to you? Before you pass the $data into the view, can you check that the $this->events->get($event_id); method is returning an object to the $data['event]` array by adding:

我假设这行$ 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();

If there's a possibility that $data['event'] could be empty you should check for it on the view using isset($event); before trying to use it in the page.

如果$ data ['event']可能为空,你应该使用isset($ event)在视图上检查它。在尝试在页面中使用它之前。

#2


1  

My guess: your error comes from this line at the end of your controllers index function

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

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

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

as far as i can tell $calendar does not contain a value for event or status_options

据我所知,$ calendar不包含event或status_options的值

#1


2  

I'm making an assumption that this line $data['event'] = $this->events->get($event_id); is the one causing you trouble, does that seem right to you? Before you pass the $data into the view, can you check that the $this->events->get($event_id); method is returning an object to the $data['event]` array by adding:

我假设这行$ 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();

If there's a possibility that $data['event'] could be empty you should check for it on the view using isset($event); before trying to use it in the page.

如果$ data ['event']可能为空,你应该使用isset($ event)在视图上检查它。在尝试在页面中使用它之前。

#2


1  

My guess: your error comes from this line at the end of your controllers index function

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

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

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

as far as i can tell $calendar does not contain a value for event or status_options

据我所知,$ calendar不包含event或status_options的值