简体   繁体   English

添加功能时出现Codeigniter错误

[英]Codeigniter error in adding functions

im creating my first site with codeigniter. 我用codeigniter创建了我的第一个站点。 Im trying to put editing function and i have error: 我试图把编辑功能,我有错误:

Severity: Notice Message: Undefined index: program Filename: controllers/news.php Line Number: 21 严重性:通知消息:未定义的索引:程序文件名:controllers / news.php行号:21

here code of controller: 这是控制器的代码:

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

Undefined index Error message is invoked when a variable or value for an array index is not defined. 未定义的索引未定义数组索引的变量或值时,将调用错误消息。

try 尝试

var_dump($data['news_item']);

Does it print the data type as Object ? 是否将数据类型打印为对象? if its object try 如果它的对象尝试

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

If these doesn't help, please post the output of the following line, This will help everyone here to fix your problem 如果这些方法没有帮助,请发布以下行的输出,这将帮助此处的每个人解决您的问题

var_dump($data['news_item']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM