Sebelum ke tahap selanjutnya yaitu membuat category,
saya sarankan untuk melihat artikel sebelumnya.
Category
a) Buat
table “category”
b) Di
“application->views->admin” buat folder “category” dan didalamnya buat
file list.php dan form.php
c) Di
“application->controllers” buat file Category.php
d) Di
“application->models” buat file Model_category.php
Model_Category.php di models
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class Model_category extends CI_Model {
function __construct() {
parent::__construct();
}
public function all() {
$sql = "SELECT * FROM category";
$query = $this->db->query($sql);
$result = $query->result();
return $result;
$query->free_result();
}
public function all_param($param) {
$sql = "SELECT * FROM category where status=$param";
$query = $this->db->query($sql);
$result = $query->result();
return $result;
$query->free_result();
}
public function find($id) {
$sql = "SELECT * FROM category WHERE id_category=?";
$query = $this->db->query($sql, array($id));
if ($query->num_rows() > 0) {
$result = $query->result();
return $result[0];
} else {
return null;
}
$query->free_result();
}
public function add($param) {
$sql = "INSERT INTO category (category, status) VALUES (?, ?)";
$this->db->query($sql, array($param['category'], $param['status']));
return true;
}
public function edit($param) {
$sql = "UPDATE category SET category=?, status=? WHERE id_category=?";
$this->db->query($sql, array($param['category'], $param['status'], $param['id_category']));
return true;
}
public function delete($id) {
$sql = "DELETE FROM category WHERE id_category = ?";
$this->db->query($sql, array($id));
return true;
}
}
Category.php di controllers
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category extends CI_Controller {
public function __construct() {
parent::__construct();
$this->isLogin();
$this->clearCache();
$this->load->model('Model_category');
}
private function isLogin() {
$isLogin = $this->session->userdata('logged_in');
if ($isLogin != 'yes' ) {
redirect(base_url('login'));
}
}
private function clearCache() {
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
}
public function index() {
$data['all'] = $this->Model_category->all();
$data['title'] = "Categories";
$data['content'] = 'admin/category/list';
$data['css'] = 'admin/template/datatables/css';
$data['js'] = 'admin/template/datatables/js';
$this->load->view('admin/template/body', $data);
}
public function add() {
$data['category'] = array(
'id_category' => '',
'category' => '',
'status' => ''
);
$data['title'] = "Add Category";
$data['content'] = 'admin/category/form';
$this->load->view('admin/template/body', $data);
}
public function edit($id_category){
$category = $this->Model_category->find($id_category);
$data['category'] = array(
'id_category' => $category->id_category,
'category' => $category->category,
'status' => $category->status
);
$data['title'] = "Edit category";
$data['content'] = 'admin/category/form';
$this->load->view('admin/template/body', $data);
}
public function save() {
$param = $this->input->post();
if ($param['id_category'] == "") {
$result = $this->Model_category->add($param);
} else {
$result = $this->Model_category->edit($param);
}
redirect(base_url('category'));
}
public function delete($id) {
$this->Model_category->delete($id);
redirect(base_url('category'));
}
}
list.php di views->category
<header class="page-header">
<div class="container-fluid">
<h2 class="no-margin-bottom"><?php echo $title;?></h2>
</div>
</header>
<!-- Breadcrumb-->
<ul class="breadcrumb">
<div class="container-fluid">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item active"><?php echo $title;?></li>
</div>
</ul>
<section class="tables">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="form-group row">
<div class="col-sm-1">
<a href="<?php echo base_url("category/add");?>" class="btn btn-primary">Add</a>
</div>
</div>
<div class="card">
<div class="card-close">
<div class="dropdown">
<button type="button" id="closeCard" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>
<div aria-labelledby="closeCard" class="dropdown-menu has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>
</div>
</div>
<div class="card-header d-flex align-items-center">
<h3 class="h4">List <?php echo $title;?></h3>
</div>
<div class="card-body">
<table width="100%" class="table table-bordered " id="dataTables-example">
<thead>
<tr class="success">
<th>#</th>
<th>Category</th>
<th>Status</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php $no=1; for ($i=0; $i < count($all); $i++) { ?>
<tr>
<th><?= $no++ ?></th>
<td><?= $all[$i]->category ?></td>
<td><?= $all[$i]->status ?></td>
<td><a href="<?php echo base_url(); ?>category/edit/<?= $all[$i]->id_category ?>" class="btn btn-sm btn-primary">Edit</a> <a href="<?php echo base_url(); ?>category/delete/<?= $all[$i]->id_category ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
form.php di views->category
<header class="page-header">
<div class="container-fluid">
<h2 class="no-margin-bottom"><?php echo $title ?></h2>
</div>
</header>
<ul class="breadcrumb">
<div class="container-fluid">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item active"><?php echo $title ?></li>
</div>
</ul>
<!-- Forms Section-->
<section class="forms">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-close">
<div class="dropdown">
<button type="button" id="closeCard" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>
<div aria-labelledby="closeCard" class="dropdown-menu has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>
</div>
</div>
<div class="card-header d-flex align-items-center">
<h3 class="h4">Form Category</h3>
</div>
<div class="card-body">
<form class="form-horizontal" action="<?php echo base_url(); ?>category/save" method="post">
<div class="form-group row">
<label class="col-sm-3 form-control-label">Category</label>
<div class="col-sm-9">
<input type="text" value="<?php echo $category['category']; ?>" class="form-control" name="category" required>
<input type="hidden" value="<?php echo $category['id_category']; ?>" class="form-control" name="id_category">
</div>
</div>
<div class="line"></div>
<div class="form-group row">
<label class="col-sm-3 form-control-label">Status</label>
<div class="col-sm-9">
<select name="status" class="form-control" required>
<option value="">Pilih Status</option>
<option value="kegiatan" <?php echo $category['status']=='kegiatan' ? 'selected' : '';?>>Kegiatan</option>
<option value="berita" <?php echo $category['status']=='berita' ? 'selected' : ''?>>Berita</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4 offset-sm-3">
<button type="submit" class="btn btn-primary">Save</button>
<button type="submit" class="btn btn-secondary">Cancel</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
0 Response to "4. Tutorial Codeigniter : Membuat Web Event Organizer (Category)"
Post a Comment