[eluser]Bramme[/eluser]
Code:
function edit($newsID = NULL)
{
$this->auth->restrict();
if (!isset($newsID))
{
redirect('/admin/news/overview');
}
$this->load->library('validation');
//rules
$this->validation->set_rules($rules);
//fields
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<li>', '</li>');
// get values from db
$this->db->where('newsID', $newsID);
$qry = $this->db->get('news');
$row = $qry->row();
foreach ($fields as $field => $name)
{
$standard_values[$field] = $row->$field;
}
$standard_values['type'] = $row->type;
if ($this->validation->run() == TRUE)
{
foreach ($fields as $field => $name)
{
$insert[$field] = $this->input->post($field);
}
$timestamp = time();
$insert['datetime'] = unix_to_human($timestamp, TRUE, 'eu');
$insert['type'] = $this->input->post('type');
if ($this->input->post('chk') == 'yes')
{
if ($_FILES['thumb_url']['error'] != 4)
{
$upload_config['upload_path'] = './_assets/images/thumbs/';
$upload_config['allowed_types'] = 'gif|jpg|png';
$upload_config['max_size'] = '100';
$upload_config['max_width'] = '150';
$upload_config['max_height'] = '150';
$this->load->library('upload', $upload_config);
if ($this->upload->do_upload('thumb_url'))
{
$info = $this->upload->data();
$insert['thumb_url'] = '/_assets/images/thumbs/'.$info['file_name'];
} else {
echo $this->upload->display_errors();
die();
}
}
else
{
$insert['thumb_url'] = '/_assets/images/thumbs/m.gif';
}
}
$this->db->where('newsID', $newsID);
if ( ! $this->db->update('news', $insert))
{
send_notice('error', 'Something went wrong with the query', 'self');
}
else
{
send_notice('success', 'Item successfully edited', 'self');
}
}
else
{
if ( ! empty($this->validation->error_string))
{
send_notice('validation_error', $this->validation->error_string);
$standard_values['poster'] = $this->input->post('poster');
$standard_values['type'] = $this->input->post('type');
$standard_values['title'] = $this->input->post('title');
$standard_values['content'] = $this->input->post('content');
}
}
$data['form'] = $this->table_editor->create_form($standard_values, TRUE);
$data['action'] = '/admin/news/edit/'.$newsID;
$this->template->add_js('_assets/js/jquery.js');
$this->template->add_js('_assets/js/news_edit.js');
$this->template->write_view('region_content', '_admin/news/new', $data);
$this->template->render();
}
That's the second method.