2014-05-10 70 views
1

即時創建與codeigniter我的第一個網站。 我試着去把編輯功能,我有錯誤:Codeigniter錯誤添加功能

嚴重性:通知消息: 未定義指數:程序 文件名:控制器/ news.php行號:控制器的21

這裏代碼:

<?php 
class News extends CI_Controller { 
public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('news_model'); 
} 
public function index() 
{ 
$data['news'] = $this->news_model->get_news(); 
$data['program'] = 'News archive'; 
$this->load->view('templates/header', $data); 
$this->load->view('templates/sidebar'); 
$this->load->view('news/index', $data); 
$this->load->view('templates/footer'); 
} 
public function view($id) 
{ 
$data = array('news_item' => $this->news_model->get_news($id)); 
$data['news_item'] = $this->news_model->get_news($id); 
$data['program'] = $data['news_item']['program']; // ERROR IS HERE 
$this->load->view('templates/header', $data); 
$this->load->view('templates/sidebar'); 
$this->load->view('news/view', $data); 
$this->load->view('templates/footer'); 
if (empty($data['news_item'])) 
{ 
    show_404(); 
} 
} 
public function create(){ 
$this->load->helper('form'); 
$this->load->library('form_validation'); 
$data['title'] = 'Создать'; 
$this->form_validation->set_rules('program', 'Программа', 'required'); 
$this->form_validation->set_rules('code', 'Код', 'required'); 
    $this->form_validation->set_rules('course', 'Направление', 'required'); 
$this->form_validation->set_rules('form', 'Форма обучения', 'required'); 
$this->form_validation->set_rules('time', 'Срок обучения', 'required'); 
    $this->form_validation->set_rules('price', 'Стоимость', 'required');  
    $this->form_validation->set_rules('accreditation', 'Аккредитация', 'required'); 
$this->form_validation->set_rules('department', 'Кафедра', 'required'); 
    $this->form_validation->set_rules('level', 'Уровень', 'required'); 
$this->form_validation->set_rules('type', 'Тип ОП', 'required'); 
if ($this->form_validation->run() === FALSE) 
{ 
    $this->load->view('templates/header', $data); 
    $this->load->view('templates/sidebar'); 
    $this->load->view('news/create'); 
    $this->load->view('templates/footer'); 
} 
else 
{ 
    $this->news_model->set_news(); 
    $this->load->view('templates/header', $data); 
    $this->load->view('templates/sidebar'); 
    $this->load->view('news/formsuccess'); 
    $this->load->view('templates/footer'); 
} 
} 
public function edit($id) { // передаем ид для редактирования 
$this->load->helper(array('form', 'url')); 
$this->load->library('validation'); 
if (!$this->input->post('id') && !$this->input->post('program')) { 
    $news_item = $this->news_model->get($id); 
    $this->validation->id = $news_item['id']; 
    $this->validation->program = $news_item['program']; 
} 
$data = array(); 
if ($this->validation->run() === FALSE) 
{ 
$this->load->view('form', $data); 
} 
else 
{ 
    $data = array(
    'program' => $this->input->post('program'), 
    'code' => $this->input->post('code'), 
    'course' => $this->input->post('course'), 
    'form' => $this->input->post('form'), 
    'time' => $this->input->post('time'), 
    'price' => $this->input->post('price'), 
    'accreditation' => $this->input->post('accreditation'), 
    'department' => $this->input->post('department'), 
    'level' => $this->input->post('level'), 
    'type' => $this->input->post('type') 
); 
    $this->news_model->update($data); 
    redirect('/news'); 
} 
} 
} 
+0

@PuzzledBoy'$數據[ 'program'] = $ data ['news_item'] ['program']; //錯誤在這裏' –

+0

這兩條線都在做同樣的工作。 '$ data = array('news_item'=> $ this-> news_model-> get_news($ id)); $ data ['news_item'] = $ this-> news_model-> get_news($ id);'擺脫任何一個並嘗試。 –

+0

檢查var_dump($ data ['program']);在控制器中。它給出了什麼輸出?張貼在這裏。 –

回答

0

未定義索引未定義數組索引的變量或值時調用錯誤消息。

嘗試

var_dump($data['news_item']); 

是否打印數據類型的對象?如果對象 嘗試

$data['program'] = $data['news_item']->program; // ERROR IS HERE 

如果這些沒有幫助,請張貼下面一行的輸出, 這將有助於在這裏給大家解決您的問題

var_dump($data['news_item']);