24 Mayıs 2010 Pazartesi

CodeIgniter Temel Veritabanı İşlemleri (CRUD) - V

Controller Dosyası (controllers/announcements.php):

[sourcecode language="php"]

<?php

class Announcements extends Controller {

var $table = 'announcements';
var $fields = array ();
var $data = array();

function Announcements ()
{
parent::Controller();
$this->load->database();
$this->load->library(array('layout', 'form_validation', 'pagination'));
$this->load->helper(array('form', 'url', 'date'));
$this->load->model('Announcements_Model');
}

function index()
{
$this->view();
}

function view ($offset = 0)
{
$this->data['records'] = $this->Announcements_Model->get_all_records($this->table, $offset);

$config['base_url'] = site_url($this->table.'/view');
$config['total_rows'] = $this->Announcements_Model->get_all_record_count($this->table);
$config['per_page'] = '10';

$this->pagination->initialize($config);

$this->layout->view($this->table.'/records', $this->data);
}

function insert ()
{
$this->data['action'] = 'insert';
if ($this->_validate())
{
$this->Announcements_Model->insert_entry($this->table, $this->fields);
$this->view();
} else
{
$this->data['title'] = '';
$this->data['body'] = '';
$this->data['date'] = unix_to_human(now());
$this->data['status'] = 1;
$this->data['position'] = '';
$this->data['color'] = '';
$this->data['password'] = '';

$this->layout->view($this->table.'/form', $this->data);
}
}

function update ($id)
{
$this->data['action'] = 'update/'.$id;
if ($this->_validate())
{
$this->Announcements_Model->update_entry($this->table, $this->fields, $id);
$this->view();
} else
{
$record = $this->Announcements_Model->get_record($this->table, $id);

$this->data['title'] = $record[0]['title'];
$this->data['body'] = $record[0]['body'];
$this->data['date'] = unix_to_human($record[0]['date']);
$this->data['status'] = $record[0]['status'];
$this->data['position'] = $record[0]['position'];
$this->data['color'] = $record[0]['color'];
$this->data['password'] = $record[0]['password'];

$this->layout->view($this->table.'/form', $this->data);
}
}

function delete ($id)
{
$this->Announcements_Model->delete_entry($this->table, $id);
$this->view();
}

function _validate ()
{
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('status', 'Status');
$this->form_validation->set_rules('position', 'Position', 'required');
$this->form_validation->set_rules('color', 'Color', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run())
{
$this->fields['title'] = $this->input->post('title');
$this->fields['body'] = $this->input->post('body');
$this->fields['date'] = human_to_unix($this->input->post('date'));
$this->fields['status'] = $this->input->post('status');
$this->fields['position'] = $this->input->post('position');
$this->fields['color'] = $this->input->post('color');
$this->fields['password'] = $this->input->post('password');

return TRUE;
} else return FALSE;
}

}
?>

[/sourcecode]

Hiç yorum yok:

Yorum Gönder