It's been a long time since I logged in here.
I do not want to bother that much so I'm not going to be writing a lot instead I'm just going to tell my problem and ask for help.
Here is my problem I'm doing a new CodeIgniter application and I'm creating the admin panel. So far I've been able to create new "page_categories" into my database and I can fetch them with not problem but when I try to update a single item for example I want to change the name of "page category 2" to whatever I want
page category 1
page category 2
the name which updates is the page category 1 , does anybody know how to fix this?
Here is my controller:
Here is my model:
and here is my view:
I do not want to bother that much so I'm not going to be writing a lot instead I'm just going to tell my problem and ask for help.
Here is my problem I'm doing a new CodeIgniter application and I'm creating the admin panel. So far I've been able to create new "page_categories" into my database and I can fetch them with not problem but when I try to update a single item for example I want to change the name of "page category 2" to whatever I want
page category 1
page category 2
the name which updates is the page category 1 , does anybody know how to fix this?
Here is my controller:
PHP Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages_categories extends CI_Controller {
public function index(){
$data['subjects'] = $this->Pages_categories_model->get_list();
// Load template
$this->template->load('admin', 'default', 'pages_categories/index', $data);
}
public function add(){
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]');
if($this->form_validation->run() == FALSE){
// Load template
$this->template->load('admin', 'default', 'pages_categories/add');
} else {
// Create Post Array
$data = array(
'name' => $this->input->post('name')
);
// Insert Subject
$this->Pages_categories_model->add($data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'subject',
'action' => 'added',
'user_id' => 1,
'message' => 'A new page category was added ('.$data["name"].')'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page category has been added');
// Redirect
redirect('admin/pages_categories');
}
}
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');
}
}
public function delete($id){
$name = $this->Pages_categories_model->get($id)->name;
// Delete Subject
$this->Pages_categories_model->delete($id);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'subject',
'action' => 'deleted',
'user_id' => 1,
'message' => 'A page category was deleted'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page category has been deleted');
// Redirect
redirect('admin/pages_categories');
}
}
Here is my model:
PHP Code:
<?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 here is my view:
PHP Code:
<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(); ?>