CodeIgniter etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
CodeIgniter etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

24 Mayıs 2010 Pazartesi

CodeIgniter Temel Veritabanı İşlemleri (CRUD) – VIII

CSS Dosyası (/css/style.css):

[sourcecode language="css"]

body
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
}

#main
{
width: 95%;
margin: auto;
}

#header
{
width:100%;
}

#content
{
width:100%;
margin: auto;
}

#footer
{
width:100%;
clear: both;
}

#box-table-a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
width: 95%;
margin:auto;
text-align:left;
border-collapse: collapse;
}

#box-table-a th
{
font-size: 13px;
font-weight: normal;
padding: 8px;
background: #b9c9fe;
border-top: 4px solid #aabcfe;
border-bottom: 1px solid #fff;
color: #039;
}

#box-table-a td
{
padding: 8px;
background: #e8edff;
border-bottom: 1px solid #fff;
color: #669;
border-top: 1px solid transparent;
}

#box-table-a tr:hover td
{
background: #d0dafd;
color: #339;
}

a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
color: #669;
text-decoration:none;
}

a:hover
{
color: #339;
text-decoration:underline;
}

h5
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
color: #669;
margin-top:5px;
margin-bottom:5px;
}

.element {
font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size:12px;
color:#339;
background-color:#fff;
border:1px #339 solid;
}

[/sourcecode]

Kaynaklar
http://www.smashingmagazine.com/2008/08/13/top-10-css-table-designs/

CodeIgniter Temel Veritabanı İşlemleri (CRUD) – VII

Layout Dosyası (views/layout_main.php):

[sourcecode language="php"]

<html>
<head>
<title><?=$title_for_layout?></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css"/>
</head>
<body>
<center>
<div id="main" >
<div id="header" ></div>
<div id="content"> <?php echo $content_for_layout?> </div>
<div id="footer" > </div>
</div>
</center>
</body>
</html>

[/sourcecode]

CodeIgniter Temel Veritabanı İşlemleri (CRUD) – VI

Layout Kütüphanesi (libraries/layout.php) [1]:

[sourcecode language="php"]

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout
{
var $obj;
var $layout;

function Layout($layout = "layout_main")
{
$this->obj =& get_instance();
$this->layout = $layout;
}

function setLayout($layout)
{
$this->layout = $layout;
}

function view($view, $data=null, $return=false)
{
$loadedData = array();
$loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);

if($return)
{
$output = $this->obj->load->view($this->layout, $loadedData, true);
return $output;
}
else
{
$this->obj->load->view($this->layout, $loadedData, false);
}
}
}
?>

[/sourcecode]

Kaynaklar
[1] http://codeigniter.com/wiki/layout_library/

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]

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

View Dosyası (views/announcements/form.php):

[sourcecode language="php"]

<?php echo form_open($this->table.'/'.$action); ?>

<table id="box-table-a">
<tr>
<td>
<h5>Title</h5>
</td>
<td>
<?php echo form_error('title'); ?>
<input type="text" name="title" value="<?php echo set_value('title', $title); ?>" size="40" />
</td>
</tr>
<tr>
<td>
<h5>Body</h5>
</td>
<td>
<?php echo form_error('body'); ?>
<textarea name="body" rows="5" cols="50"><?php echo set_value('body', $body);?></textarea>
</td>
</tr>
<tr>
<td>
<h5>Date</h5>
</td>
<td>
<?php echo form_error('date'); ?>
<input type="text" name="date" value="<?php echo set_value('date', $date); ?>" size="40" />
</td>
</tr>
<tr>
<td>
<h5>Status</h5>
</td>
<td>
<?php echo form_error('status'); ?>
<input type="checkbox" name="status" value="1" <?php echo set_checkbox('status', 1, $status == 1 ? TRUE : FALSE); ?> />
</td>
</tr>
<tr>
<td>
<h5>Position</h5>
</td>
<td>
<?php echo form_error('position'); ?>
<select name="position">
<option value="left" <?php echo set_select('position', 'left', $position == 'left' ? TRUE : FALSE); ?> >Left</option>
<option value="right" <?php echo set_select('position', 'right', $position == 'right' ? TRUE : FALSE); ?> >Right</option>
<option value="top" <?php echo set_select('position', 'top', $position == 'top' ? TRUE : FALSE); ?> >Top</option>
</select>
</td>
</tr>
<tr>
<td>
<h5>Color</h5>
</td>
<td>
<?php echo form_error('color'); ?>
<input type="radio" name="color" value="red" <?php echo set_radio('color', 'red', $color == 'red' ? TRUE : FALSE); ?> >Red
<input type="radio" name="color" value="blue" <?php echo set_radio('color', 'blue', $color == 'blue' ? TRUE : FALSE); ?> >Blue
<input type="radio" name="color" value="green" <?php echo set_radio('color', 'green', $color == 'green' ? TRUE : FALSE); ?> >Green
</td>
</tr>
<tr>
<td>
<h5>Password</h5>
</td>
<td>
<?php echo form_error('password'); ?>
<input type="password" name="password" value="" size="40" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="OK"/>
</td>
</tr>
</table>

</form>

[/sourcecode]

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

View Dosyası (views/announcements/records.php):

[sourcecode language="php"]

<?php echo anchor($this->table.'/insert', '[Insert Record]').'<p/>'; ?>

<table id="box-table-a">
<tr>
<th>Title</th>
<th>Date</th>
<th>Status</th>
<th>Position</th>
<th>Color</th>
<th>Password</th>
<th colspan="2">Action</th>
</tr>

<?php
foreach ($records as $rec)
{
echo '<tr>';
echo '<td>'.$rec['title'].'</td>';
echo '<td>'.unix_to_human($rec['date']).'</td>';
echo '<td>'.$rec['status'].'</td>';
echo '<td>'.$rec['position'].'</td>';
echo '<td>'.$rec['color'].'</td>';
echo '<td>'.$rec['password'].'</td>';
echo '<td>'.anchor($this->table.'/update/'.$rec['id'], '[edit]').'</td>';
echo '<td>'.anchor($this->table.'/delete/'.$rec['id'], '[delete]', array('onclick'=>"return confirm('Are you sure?')")).'</td>';
echo '</tr>';
}
?>

</table><p/>

<?php echo $this->pagination->create_links(); ?>

[/sourcecode]

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

Model Dosyası (models/announcements_model.php):

[sourcecode language="php"]

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Announcements_Model extends Model {

function Announcements_Model()
{
parent::Model();
}

function get_all_records($table, $offset)
{
return $this->db->get($table, 10, $offset)->result_array();
}

function get_record ($table, $id)
{
$this->db->where('id', $id);
return $this->db->get($table)->result_array();
}

function get_all_record_count($table)
{
return $this->db->count_all($table);
}

function insert_entry($table, $data)
{
$this->db->insert($table, $data);
}

function update_entry($table, $data, $id)
{
$this->db->update($table, $data, array('id' => $id));
}

function delete_entry($table, $id)
{
return $this->db->delete($table, array('id' => $id)) ? TRUE : FALSE;
}

}

[/sourcecode]

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

Tablo:

announcements

[sourcecode language="sql"]

CREATE TABLE IF NOT EXISTS `announcements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`body` text NOT NULL,
`date` int(11) NOT NULL,
`status` char(1) NOT NULL,
`position` varchar(10) NOT NULL,
`color` varchar(15) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

[/sourcecode]

Dizin Yapısı:

application/models/announcements_model.php
application/views/announcements/recors.php
application/views/announcements/form.php
application/controllers/announcements.php
application/libraries/layout.php
application/views/layout_main.php
/css/style.css

FormIgniter - Easy Form Generator

CodeIgniter çatısı için Ollie Rattue tarafından geliştirilmiş; ağırlıklı olarak MVC formlarını otomatik olarak oluşturması için düşünülmüş güzel bir uygulama: http://formigniter.org/

Uygulamanız için belirttiğiniz field adları ile tablonuz için SQL kodları, Controller, Form ve Model dosyaları otomatik üretiliyor ve indirilebiliyor.