问题描述:

I have been trying to learn CI and implement a blog content management system following a lightweight tutorial I found online today, but I'm currently experiencing some problems in the view.

Adding new entries "works", but I get the following errors on top of the add_new page:

http://pastebin.com/4UhMTqLj

CONTROLLER

function new_entry()

{

$this->load->helper('form');

$this->load->library(array('form_validation','session'));

//set validation rules

$this->form_validation->set_rules('entry_name', 'Title', 'required|xss_clean|max_length[200]');

$this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');

if ($this->form_validation->run() == FALSE)

{

$this->load->view('header', $data);

$this->load->view('admin/new_entry', $data);

$this->load->view('footer', $data);

}

else

{

//if valid

$name = $this->input->post('entry_name');

$body = $this->input->post('entry_body');

$this->articles_model->new_entry($name,$body);

$this->session->set_flashdata('message', '1 new entry added!');

redirect('articles/new_entry');

}

}

VIEW

<h2>Add new entry</h2>

<?php echo validation_errors(); ?>

<?php if($this->session->flashdata('message')){echo $this->session->flashdata('message');}?>

<?php echo form_open('articles/new_entry');?>

<p>Title:<br />

<input type="text" name="entry_name" />

</p>

<p>Body:<br />

<textarea name="entry_body" rows="5" cols="50" style="resize:none;"></textarea>

</p>

<input type="submit" value="Submit" />

<?php echo form_close();?>

MODEL

function new_entry($name,$body)

{

$data = array(

'entry_name' => $name,

'entry_body' => $body

);

$this->db->insert('entry',$data);

}

What can be the cause?

Edit: I'm using Codeigniter 3.

网友答案:

You must initiate the $data variable before passing in to the views.

eg. $data = array(); put it on the new entry class

网友答案:

I completely agree with the answer from Cha Hernandez, however, another possibility would be:

To remove the $data variable altogether:

   $this->load->view('header');
    $this->load->view('admin/new_entry');
    $this->load->view('footer');

This is mainly because you're not actually passing any information to the view, so there is no point in having it in the instance.

Hope this helps!

网友答案:

Change your CONTROLLER TO file below code where i removed variable $data since it is not assigned to any values and passing undefined variable $data is not necessary.

function new_entry()
    {
        $this->load->helper('form');
        $this->load->library(array('form_validation','session'));

        //set validation rules
        $this->form_validation->set_rules('entry_name', 'Title', 'required|xss_clean|max_length[200]');
        $this->form_validation->set_rules('entry_body', 'Body', 'required|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('header');
            $this->load->view('admin/new_entry');
            $this->load->view('footer');
        }
        else
        {
            //if valid
            $name = $this->input->post('entry_name');
            $body = $this->input->post('entry_body');
            $this->articles_model->new_entry($name,$body);
            $this->session->set_flashdata('message', '1 new entry added!');
            redirect('articles/new_entry');
        }
    }
相关阅读:
Top