Create a new file in a folder -> Models
1 |
/application/models/site_mod.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Site_mod extends CI_Model { function getAll() { $query = $this->db->query("SELECT * FROM data "); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[] = $row; } return $data; } else { echo "Data not exist"; } } function get_records() { $query = $this->db->get('data'); return $query->result(); } function add_record($data) { $this->db->insert('data', $data); return; } function update_record($data) { $this->db->where('id', 2); $this->db->update('data', $data); } function delete_record() { $this->db->where('id', $this->uri->segment(3)); $this->db->delete('data'); } } |
Create a new file in a folder -> Controller
1 |
/application/controller/site.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Site extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('site_model'); } function index() { if ($query = $this->site_model->get_records()) { $data['records'] = $query; } $this->load->view('option_view', $data); } function create() { $this->load->library('form_validation'); $this->form_validation->set_rules('title', 'Title', 'trim|required'); $this->form_validation->set_rules('content', 'Content', 'trim|required'); if ($this->form_validation->run() == FALSE) { $this->load->view('option_view'); } else { $data = array( 'title' => $this->input->post('title'), 'contents' => $this->input->post('content'), 'author' => 'Indra' ); $this->site_model->add_record($data); $this->index(); } } function delete() { $this->site_model->delete_record(); $this->index(); } function update() { $data = array ( 'title' => 'Cinta bergelora', 'contents' => 'Cinta yang terlarang', 'author' => 'Indri' ); $this->site_model->update_record($data); } } |
Create a new file in a folder -> Views
1 |
/application/views/option_view.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Option View</title> <style type="text/css"> label { display: block; } </style> </head> <body> <h2>Create</h2> <?php echo form_open('site/create'); ?> <p> <label for="title">Title: </label> <input type="text" name="title" value="<?php echo set_value('title'); ?>" /> </p> <p> <label for="content">Content: </label> <textarea name="content"></textarea> </p> <p> <?php echo form_submit('submit', 'Submit'); ?> </p> <?php echo form_close(); ?> <?php echo validation_errors(); ?> <br /> <?php if (isset($records)) : foreach ($records as $row) : ?> <a href='<?php echo 'site/delete/' . $row->id; ?>'><h2><?php echo $row->title; ?></h2></a> <p><?php echo $row->contents; ?></p> <?php endforeach ?> <?php else : ?> <h2>Data not found</h2> <?php endif; ?> </body> </html> |