I wrote the code below, but it give me several errors (listed after the code). I didn’t know how to solve it.
Model -> News_model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($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();
}
}
?> ```
**Controller -> News.php **
```<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('News_model');
$this->load->helper('url_helper');
}
public function index()
{
//$this->load->model('News_model');
$data['news'] = $this->News_model->get_news();
$data['title'] = 'News archive';
$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->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
views -> views/news/create.php
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="text" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
view -> views/news/index.php
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>
<?php endforeach; ?>
views -> views/news/success.php
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Succeed</h2>
</body>
</html>
views-> views/news/view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
Errors
A PHP Error was encountered Severity: Notice
Message: Undefined property: News::$news_model
Filename: controllers/News.php
Line Number: 72
Backtrace:
File: C:\xampp\htdocs\oproject\application\controllers\News.php Line:
72 Function: _error_handlerFile: C:\xampp\htdocs\oproject\index.php Line: 315 Function:
require_onceAn uncaught Exception was encountered Type: Error
Message: Call to a member function set_news() on null
Filename: C:\xampp\htdocs\oproject\application\controllers\News.php
Line Number: 72
Backtrace:
File: C:\xampp\htdocs\oproject\index.php Line: 315 Function:
require_once
Change news_model into News_model here
$this->News_model->set_news();
$this->load->view('news/success');
Answer:
Please load your model in controller where News_model using
$this->load->model('News_model');
and use
$this->News_model->set_news();
in create()
method