A
A
artem36012022-02-25 21:50:01
CodeIgniter
artem3601, 2022-02-25 21:50:01

Who can help with the code on Codeigniter?

Here is my code:

<?php

defined('BASEPATH') OR exit('No direc script access allowed');

class News extends CI_Controller {
    
    public function __construct(){
        parent::__construct();
        $this->load->model('news_model');
    }

    public function index() {
        $data['title'] = 'Все новости';
        $data['news'] = $this->news_model->getNews();

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($slug = NULL) {
        $data['news_item'] = $this->news_model->getNews($slug);

        if (empty($data['news_item'])) {
            show_404();
        }

        $data['title'] = $data['news_item']['title'];
        $data['content'] = $data['news_item']['text'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }

    public function create() {
        $data['title'] = "Добавить новость";

        if ($this->input->post('slug') && $this->input->post('title') && $this->input->post('text')) {

            $slug=$this->input->post('slug');
            $title=$this->input->post('title');
            $text=$this->input->post('text');

            if ($this->news_model->setNews($slug, $title, $text)) {
                $this->load->view('templates/header', $data);
                $this->load->view('news/success', $data);
                $this->load->view('templates/footer');
            } else {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create', $data);
                $this->load->view('templates/footer');
            }
        }


    }

    public function edit($slug = NULL) {
        $data['title'] = "редактировать новость";
        $data['news_item'] = $this->news_model->getNews($slug);

/*      if(empty($data['news_item'])) {
            show_404();
        }*/

        $data['title_news'] = $data['news_item']['title'];
        $data['content_news'] = $data['news_item']['text'];
        $data['slug_news'] = $data['news_item']['slug'];

        if($this->input->post('slug') && $this->input->post('title') && $this->input->post('text')) {
            $slug = $this->input->post('slug');
            $title = $this->input->post('title');
            $text = $this->input->post('text');

            if($this->news_model->updateNews($slug, $title, $text)) {
                echo "Новость успешно отредактирована";
            }
        }

        $this->load->view('templates/header', $data);
        $this->load->view('news/edit', $data);
        $this->load->view('templates/footer');

    }
}

And I get these errors:

A PHP Error was encountered
Severity: Warning

Message: Trying to access array offset on value of type null

Filename: controllers/News.php

Line Number: 67

Backtrace:

File: C:\xampp\htdocs\kinomonster\application\controllers\News.php
Line: 67
Function: _error_handler

File: C:\xampp\htdocs\kinomonster\index.php
Line: 315
Function: require_once



A PHP Error was encountered
Severity: Warning

Message: Trying to access array offset on value of type null

Filename: controllers/News.php

Line Number: 68

Backtrace:

File: C:\xampp\htdocs\kinomonster\application\controllers\News.php
Line: 68
Function: _error_handler

File: C:\xampp\htdocs\kinomonster\index.php
Line: 315
Function: require_once



A PHP Error was encountered
Severity: Warning

Message: Trying to access array offset on value of type null

Filename: controllers/News.php

Line Number: 69

Backtrace:

File: C:\xampp\htdocs\kinomonster\application\controllers\News.php
Line: 69
Function: _error_handler

File: C:\xampp\htdocs\kinomonster\index.php
Line: 315
Function: require_once


Can you please tell me what to do.

Here is my model code:

<?php 

  class News_model extends CI_Model {
    
    public function __construct() {
      $this->load->database();
    }

    public function getNews($slug = FALSE){
      if ($slug === FALSE) {
        $query = $this->db->get('news');
        return $query->result_array();
      }

      $query = $this->db->get_where('news', array('slug' => $slug));
      return $query->row_array();
    }

    public function setNews($slug, $title, $text) {
      $data = array (
        'title' => $title,
        'slug' => $slug,
        'text' => $text
      );

      return $this->db->insert('news', $data);
    }		

  public function updateNews($slug, $title, $text) {

    $data = array(
      'title' => $title,
      'slug' => $slug, 
      'text' => $text
    );

    return $this->db->update('news', $data, array('slug' => $slug));

  }

  }
 ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question