Anda di halaman 1dari 6

SIMPLE CRUD CODE IGNITER

Pemrograman 4
STMIK Bandung

Model News

<?php
class News_model extends CI_Model {

public function __construct()


{
$this->load->database();
}

public function get_news($slug = FALSE)


{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}

$query = $this->db->get_where('news', array('slug' => $slug));


return $query->row_array();
}

public function get_news_by_id($id = 0)


{
if ($id === 0)
{
$query = $this->db->get('news');
return $query->result_array();
}

$query = $this->db->get_where('news', array('id' => $id));


return $query->row_array();
}

public function set_news($id = 0)


{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);

if ($id == 0) {
SIMPLE CRUD CODE IGNITER
Pemrograman 4
STMIK Bandung

return $this->db->insert('news', $data);


} else {
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}

public function delete_news($id)


{
$this->db->where('id', $id);
return $this->db->delete('news');
}
}

Controller News

<?php
class News extends CI_Controller {

public function __construct()


{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}

public function index()


{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';

$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}

public function view($slug = NULL)


{
$data['news_item'] = $this->news_model->get_news($slug);

if (empty($data['news_item']))
{
show_404();
}

$data['title'] = $data['news_item']['title'];
SIMPLE CRUD CODE IGNITER
Pemrograman 4
STMIK Bandung

$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}

public function create()


{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a news item';

$this->form_validation->set_rules('title', 'Title', 'required');


$this->form_validation->set_rules('text', 'Text', 'required');

if ($this->form_validation->run() === FALSE)


{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');

}
else
{
$this->news_model->set_news();
$this->load->view('templates/header', $data);
$this->load->view('news/success');
$this->load->view('templates/footer');
}
}

public function edit()


{
$id = $this->uri->segment(3);

if (empty($id))
{
show_404();
}

$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Edit a news item';


$data['news_item'] = $this->news_model->get_news_by_id($id);
SIMPLE CRUD CODE IGNITER
Pemrograman 4
STMIK Bandung

$this->form_validation->set_rules('title', 'Title', 'required');


$this->form_validation->set_rules('text', 'Text', 'required');

if ($this->form_validation->run() === FALSE)


{
$this->load->view('templates/header', $data);
$this->load->view('news/edit', $data);
$this->load->view('templates/footer');

}
else
{
$this->news_model->set_news($id);
//$this->load->view('news/success');
redirect( base_url() . 'index.php/news');
}
}

public function delete()


{
$id = $this->uri->segment(3);

if (empty($id))
{
show_404();
}

$news_item = $this->news_model->get_news_by_id($id);

$this->news_model->delete_news($id);
redirect( base_url() . 'index.php/news');
}
}

View index.php

<h2><?php echo $title; ?></h2>

<table border='1' cellpadding='4'>


<tr>
<td><strong>Title</strong></td>
<td><strong>Content</strong></td>
<td><strong>Action</strong></td>
</tr>
<?php foreach ($news as $news_item): ?>
SIMPLE CRUD CODE IGNITER
Pemrograman 4
STMIK Bandung

<tr>
<td><?php echo $news_item['title']; ?></td>
<td><?php echo $news_item['text']; ?></td>
<td>
<a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View</a> |
<a href="<?php echo site_url('news/edit/'.$news_item['id']); ?>">Edit</a> |
<a href="<?php echo site_url('news/delete/'.$news_item['id']); ?>" onClick="return
confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>

View view.php

<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
?>

View create.php

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>


<table>
<tr>
<td><label for="title">Title</label></td>
<td><input type="input" name="title" size="50" /></td>
</tr>
<tr>
<td><label for="text">Text</label></td>
<td><textarea name="text" rows="10" cols="40"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Create news item" /></td>
</tr>
</table>
</form>

View edit.php
SIMPLE CRUD CODE IGNITER
Pemrograman 4
STMIK Bandung

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/edit/'.$news_item['id']); ?>


<table>
<tr>
<td><label for="title">Title</label></td>
<td><input type="input" name="title" size="50" value="<?php echo $news_item['title'] ?>"
/></td>
</tr>
<tr>
<td><label for="text">Text</label></td>
<td><textarea name="text" rows="10" cols="40"><?php echo $news_item['text']
?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Edit news item" /></td>
</tr>
</table>
</form>

View success.php

<p>News added successfully!</p>

Anda mungkin juga menyukai