I've been working on a new CodeIgniter application and so far I've been able to create "page_categories" into the table, now my problem is when I try to update a certain category.
Let's say that I have two records on page_categories table:
Ok, now to be more specific when I try to change the name of "Page Category Two" rather than changing that name, my form updates the first record "Page Category One". Any idea how to fix this?
This is what I have on my controller:
public function edit($id){
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]');
if($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->Pages_categories_model->get($id);
// Load template
$this->template->load('admin', 'default', 'pages_categories/edit', $data);
} else {
$old_name = $this->Pages_categories_model->get($id)->name;
$new_name = $this->input->post('name');
// Create Post Array
$data = array(
'name' => $this->input->post('name')
);
// Insert Subject
$this->Pages_categories_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'subject',
'action' => 'updated',
'user_id' => 1,
'message' => 'A page category ('.$old_name.') was renamed to ('.$new_name.')'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page category has been updated');
//Redirect
redirect('admin/pages_categories');
}
}
and this is what I have on my model:
<?php
class Pages_categories_model extends CI_MODEL{
function __construct(){
parent::__construct();
$this->table = 'pages_categories';
}
public function get_list(){
$query = $this->db->get($this->table);
return $query->result();
}
public function get($id){
$query = $this->db->get($this->table);
$this->db->where('id', $id);
return $query->row();
}
public function add($data){
$this->db->insert($this->table, $data);
}
public function update($id, $data){
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function delete($id){
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
and finally my view:
<h2 class="page-header">Edit Page Category</h2>
<?php echo form_open('admin/pages_categories/edit/'.$item->id); ?>
<div class="form-group">
<?php echo form_label('Page Category Name', 'name'); ?>
<?php
$data = array(
'name' => 'name',
'id' => 'name',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->name,
);
?>
<?php echo form_input($data); ?>
</div>
<?php echo form_submit('mysubmit', 'Update Page Category', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
I would like to say that my flash data message is not working as I expected. (It's not that important though.)
I want the flash-data message to be displayed in a different view; here it is my view:
<h2 class="page-header">Page Categories</h2>
<?php $CI =& get_instance(); if($CI->session->flashdata('success'){ ?>
<div class="alert alert-success">
<strong>Success!! </strong><?php echo $this->session->flashdata('success'); ?>
</div>
<?php } ?>
<?php if($this->session->flashdata('error')) : ?>
<?php echo '<div class="alert alert-danger">'.$this->session->flashdata('error').'</div>'; ?>
<?php endif; ?>
<?php if($subjects) : ?>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Date Created</th>
<th></th>
</tr>
<?php foreach($subjects as $subject) : ?>
<?php $date = strtotime($subject->create_date); ?>
<?php $formatted_date = date('F j, Y, g:i a', $date); ?>
<tr>
<td><?php echo $subject->id; ?></td>
<td><?php echo $subject->name; ?></td>
<td><?php echo $formatted_date; ?></th>
<td>
<?php echo anchor('admin/pages_categories/edit/'.$subject->id.'', 'Edit', 'class="btn btn-default"'); ?>
<?php echo anchor('admin/pages_categories/delete/'.$subject->id.'', 'Delete', 'class="btn btn-danger"'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php else : ?>
<div class="alert alert-danger">No Page Category Found</div>
<?php endif; ?>
</div>