You now know how you can read data from a database using CodeIgnite, but
you haven't written any information to the database yet. In this
section you'll expand your news controller and model created earlier to
include this functionality.
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?> //validations in codeigniter.
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
There are only two things here that probably look unfamiliar to you: the form_open() function and the validation_errors() function.
Views
<h2>Create a news item</h2><?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?> //validations in codeigniter.
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
There are only two things here that probably look unfamiliar to you: the form_open() function and the validation_errors() function.
Controller
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');
}
}
Model
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);
}