Hello all, first approach in codeigniter - El Forum - 04-14-2012
[eluser]Unknown[/eluser]
I followed the guide on website [CI 2.1] "the mini-tutorial" that explains how to create news by form, now i want to create the "update" method but i have a difficult with le logic...
my models:
Code: <?php
#inizio il modello per le news
class News_model extends CI_Model{
public function __construct()
{
$this->load->database();
}
// prelevo i record dalla tabella
public function get_news($id = FALSE)
{
if ($id === FALSE)
{
$this->db->order_by("title", "asc");
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
// scivo i record nella tabella
public function set_news()
{
$this->load->helper('url');
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('slug'),
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
// modifico i record gia esistenti in una tabella
public function update_news($id)
{
$this->load->helper('url');
$data = array(
'title' => $title,
'slug' => $slug,
'text' => $text
);
return $this->db->update('news', $data, array('id' => $id));
}
}
my controller:
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
#il controller delle news
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['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($id)
{
$data['news_item'] = $this->news_model->get_news($id);
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');
}
// funzione per creare record
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');
}
}
// funzione per modificare i record
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update an intem';
$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/update');
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news($id);
$this->load->view('news/success');
}
}
}
and the form view:
Code: <h2>Update an item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="slug">Slug</label>
<input type="input" name="slug" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Update an item" />
</form>
the error is a 404page when i go tonews/update/1 or any other ID...
any one can help me?
|