1

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!

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 Answers 2

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);    
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.

Sign up to request clarification or add additional context in comments.

8 Comments

This is the error I received after putting in the die function. A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Planner::update() Filename: user/planner.php Line Number: 147 A PHP Error was encountered Severity: Notice Message: Undefined variable: event_id Filename: user/planner.php Line Number: 176 array(1) { ["event"]=> bool(false) }
I get the database information though when I type out example.com/folder/controller/method/(some id) like so array(1) { ["event"]=> object(stdClass)#27 (9) { ["id"]=> string(1) "NULL" ["status"]=> NULL ["type"]=> string(6) "Content" ["title"]=> string(6) "NULL" ["content"]=> string(0) "NULL" ["user_id"]=> string(1) "#" ["date"]=> string(10) "NULL" ["created"]=> string(10) "NULL" ["modified"]=> NULL } } Disclaimer: I changed some content out with NULL for security purposes.
Is the $event_id definitely set when you call $this->events->get($event_id);?
No, it is not set. Just tested it in view. Sorry about the miscommunication there.
No worries. Is that solved now or are you still having the original problem?
|
1

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

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

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

1 Comment

It was a javascript error, along with the view. Thanks a lot for all of your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.