Advertisement
yhoezt_27

Untitled

Oct 2nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | None | 0 0
  1. Contoh Operasi CRUD Codeigniter :
  2.  
  3. VIEW
  4. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
  5.  
  6. <div class="page-title">
  7. <div class="title_left">
  8. <h2>
  9. <i><a href="#"><?php echo $kategori?></a></i>
  10. <i class="fa fa-angle-double-right"></i>
  11. <a href="<?php echo site_url($alamat)?>"><?php echo $title?></a>
  12. </h2>
  13. </div>
  14. <div class="title_right">
  15. <div class="col-md-8 col-sm-8 col-xs-8">
  16. <button type="button" id="btn-tambah" onclick="add()" class="btn btn-success pull-right">
  17. <span class="glyphicon glyphicon-plus"></span> Tambah Data
  18. </button>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="clearfix"></div>
  23. <br />
  24.  
  25. <div class="row">
  26. <div class="col-md-10 col-sm-10 col-xs-10">
  27. <div class="x-panel">
  28. <div class="x_title">
  29. <h2><?php echo $title?></h2>
  30.  
  31. <div class="clearfix"></div>
  32. </div>
  33. <div class="x_content">
  34. <div id="view">
  35. <table id="brand" class="table table-bordered table-striped table-hover">
  36. <thead>
  37. <tr class="headings">
  38. <th>ID</th>
  39. <th>Brand</th>
  40. <th>Buatan</th>
  41. <th>Updated</th>
  42. <th>Keterangan</th>
  43. <th colspan = "2" class="text-center"><span class="glyphicon glyphicon-cog"></span></th>
  44. </tr>
  45. </thead>
  46. <tbody>
  47. <?php
  48. if ($data_brand->num_rows() > 0)
  49. {
  50. $i=1;
  51. foreach($data_brand->result_array() as $keys=>$key)
  52. {
  53. if ($i%2 == 0)
  54. $class = "odd pointer";
  55. else $class = "even pointer";
  56. ?>
  57. <tr class=<?php echo $class?>>
  58. <td class="align-middle"><?php print $key['id_brand']?></td>
  59. <td class="align-middle"><?php print $key['brand']?></td>
  60. <td class="align-middle"><?php print $key['made']?></td>
  61. <td class="align-middle"><?php print $key['updated']?></td>
  62. <td class="align-middle"><?php print $key['keterangan']?></td>
  63. <td class="align-middle text-center">
  64. <a href="javascript:void();" onclick="edit(<?php echo $key['id_brand']; ?>);" class="btn btn-default" title="Edit"><span class="glyphicon glyphicon-pencil"></span></a>
  65. </td>
  66. <td class="align-middle text-center">
  67. <a href="javascript:void();" onclick="hapus(<?php echo $key['id_brand']; ?>);" class="btn btn-danger" title="Delete"><span class="glyphicon glyphicon-erase"></span></a>
  68. </td>
  69. </tr>
  70. <?php
  71. $i++;
  72. }
  73. }
  74. ?>
  75. </tbody>
  76. </table>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82.  
  83.  
  84.  
  85. <script>
  86. $(document).ready( function () {
  87. $('#brand').DataTable();
  88. });
  89.  
  90. var save_method;
  91. var table;
  92.  
  93. function add()
  94. {
  95. save_method = 'add';
  96. $('#form')[0].reset(); // reset form on modals
  97. $('#form-modal').modal('show'); // show bootstrap modal
  98. //$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal title
  99. }
  100.  
  101. function edit(id)
  102. {
  103. save_method = 'update';
  104. $('#form')[0].reset(); // reset form on modals
  105.  
  106. //Ajax Load data from ajax
  107. $.ajax({
  108. url : "<?php echo site_url('brand/ajax_edit/')?>/" + id,
  109. type: "GET",
  110. dataType: "JSON",
  111. success: function(data)
  112. {
  113. $('[name="id_brand"]').val(data.id_brand).attr("readonly","readonly");
  114. $('[name="brand"]').val(data.brand).attr("readonly","readonly");
  115. $('[name="made"]').val(data.made);
  116. $('[name="keterangan"]').val(data.keterangan);
  117.  
  118. $('#form-modal').modal('show'); // show bootstrap modal when complete loaded
  119. $('.modal-title').text('Edit Brand'); // Set title to Bootstrap modal title
  120.  
  121. },
  122. error: function (jqXHR, textStatus, errorThrown)
  123. {
  124. alert('Error get data from ajax');
  125. }
  126. });
  127. }
  128.  
  129. function save()
  130. {
  131. var url;
  132. if(save_method == 'add')
  133. {
  134. url = "<?php echo site_url('brand/tambah')?>";
  135. }
  136. else
  137. {
  138. url = "<?php echo site_url('brand/edit')?>";
  139. }
  140.  
  141. // ajax adding data to database
  142. $.ajax({
  143. url : url,
  144. type: "POST",
  145. data: $('#form').serialize(),
  146. dataType: "JSON",
  147. success: function(data)
  148. {
  149. //if success close modal and reload ajax table
  150. $('#form_modal').modal('hide');
  151. location.reload();// for reload a page
  152. },
  153. error: function (jqXHR, textStatus, errorThrown)
  154. {
  155. alert('Error adding / update data');
  156. }
  157. });
  158. }
  159.  
  160. function hapus(id)
  161. {
  162. if(confirm('delete this data?'))
  163. {
  164. // ajax delete data from database
  165. $.ajax({
  166. url : "<?php echo site_url('brand/konfirm')?>/"+id,
  167. type: "POST",
  168. dataType: "JSON",
  169. beforeSend: function(e) {
  170. if(e && e.overrideMimeType) {
  171. e.overrideMimeType("application/json;charset=UTF-8");
  172. }
  173. },
  174. success: function(response)
  175. {
  176. alert(response.pesan);
  177. $("#view").html(response.html);
  178. location.href = location.pathname;
  179. },
  180. error: function (jqXHR, textStatus, errorThrown)
  181. {
  182. alert('Error deleting data');
  183. }
  184. });
  185.  
  186. }
  187. }
  188.  
  189. $(document).ready(function(){
  190.  
  191. $('#form-modal').on('hidden.bs.modal', function (e){
  192. $("#btn-reset").click();
  193. $("#brand").removeAttr('readonly');
  194. });
  195. });
  196. </script>
  197.  
  198. <!-- Bootstrap modal -->
  199. <div class="modal fade" id="form-modal" role="dialog">
  200. <div class="modal-dialog">
  201. <div class="modal-content">
  202. <div class="modal-header">
  203. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  204. <h3 class="modal-title">Form Data Brand</h3>
  205. </div>
  206. <div class="modal-body form">
  207. <form action="#" id="form" class="form-horizontal">
  208. <input type="hidden" value="" name="id_brand"/>
  209. <div class="form-body">
  210. <div class="form-group">
  211. <label class="control-label col-md-3">Brand</label>
  212. <div class="col-md-9">
  213. <input name="brand" placeholder="Nama Brand" class="form-control" type="text">
  214. </div>
  215. </div>
  216. <div class="form-group">
  217. <label class="control-label col-md-3">Made/Buatan</label>
  218. <div class="col-md-9">
  219. <input name="made" placeholder="Made/Buatan" class="form-control" type="text">
  220. </div>
  221. </div>
  222. <div class="form-group">
  223. <label class="control-label col-md-3">Keterangan</label>
  224. <div class="col-md-9">
  225. <textarea rows="6" cols="3" name="keterangan" class="form-control"></textarea>
  226. </div>
  227. </div>
  228. </div>
  229. </form>
  230. </div>
  231. <div class="modal-footer">
  232. <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
  233. <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
  234. </div>
  235. </div><!-- /.modal-content -->
  236. </div><!-- /.modal-dialog -->
  237. </div><!-- /.modal -->
  238. <!-- End Bootstrap modal -->
  239.  
  240. CONTROLLER
  241. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  242.  
  243. class Brand extends CI_Controller
  244. {
  245. private $table = 'tb_brand';
  246. private $primary = 'id_brand';
  247.  
  248. function __construct()
  249. {
  250. parent::__construct();
  251. $this->load->library(array('template'));
  252.  
  253. $user = $this->session->userdata('username');
  254. if ($user)
  255. {
  256. $lvl = $this->session->userdata('level');
  257. if($lvl != '1')
  258. {
  259. redirect('dashboard/index');
  260. }
  261. }
  262. else redirect('web');
  263. }
  264.  
  265. function index()
  266. {
  267. $lvl = $this->session->userdata('level');
  268. $kategori = $this->_get_nama($lvl);
  269.  
  270. $data_brand = $this->m_all->semua($this->table);
  271.  
  272. $data['kategori'] = $kategori;
  273. $data['alamat'] = 'brand/index';
  274. $data['data_brand'] = $data_brand;
  275. $data['title']="List Brand";
  276.  
  277. $this->template->display('brand/index',$data);
  278. }
  279.  
  280. function tambah()
  281. {
  282. $brand = db_clean($this->input->post('brand'));
  283. $made = db_clean($this->input->post('made'));
  284.  
  285. if ($brand and $made)
  286. {
  287. $cek = $this->m_all->cari($brand, $made, '','','','brand','made','',$this->table);
  288. if ($cek->num_rows() > 0)
  289. $response = array('status'=>'gagal','pesan'=>'Data brand telah tersedia');
  290. else
  291. {
  292. $info = array(
  293. 'id_brand' => $this->auth->get_jumlah($this->primary, $this->table) + 1,
  294. 'brand' => $brand,
  295. 'made' => $made,
  296. 'keterangan' => db_clean($this->input->post('keterangan')),
  297. 'updated' => $this->session->userdata('nama').'-'.date('Y-m-d H:i:s')
  298. );
  299.  
  300. $this->m_all->simpan($info, $this->table);
  301.  
  302. ob_start();
  303. $html = ob_get_contents();
  304. ob_end_clean();
  305.  
  306. $response = array('status'=>'true','pesan'=>'Data berhasil disimpan', 'html'=>$html);
  307. }
  308. }
  309. else $response = array('status'=>'false','pesan'=>'Data brand dan Made kosong');
  310.  
  311. echo json_encode($response);
  312. }
  313.  
  314. function ajax_edit($id)
  315. {
  316. $data = $this->m_all->get_by_id($this->table, $this->primary, $id);
  317.  
  318. echo json_encode($data);
  319. }
  320.  
  321. function edit()
  322. {
  323. $kode = $this->input->post('id_brand');
  324. $made = db_clean($this->input->post('made'));
  325.  
  326. if ($kode and $made)
  327. {
  328. $cek = $this->m_all->cek($kode, $this->primary, $this->table);
  329. foreach ($cek->result() as $row)
  330. {
  331. $updated = $row->updated;
  332. }
  333.  
  334. $info = array(
  335. 'made' => $made,
  336. 'keterangan' => db_clean($this->input->post('keterangan')),
  337. 'updated' => $updated.';'.$this->session->userdata('nama').'-'.date('Y-m-d H:i:s')
  338. );
  339.  
  340. $this->m_all->update($kode, $info, $this->primary, $this->table);
  341.  
  342. ob_start();
  343. $html = ob_get_contents();
  344. ob_end_clean();
  345.  
  346. $response = array('status'=>'true','pesan'=>'Data berhasil di-ubah', 'html'=>$html);
  347. }
  348. else $response = array('status'=>'false','pesan'=>'Data gagal di-ubah');
  349.  
  350. echo json_encode($response);
  351. }
  352.  
  353. function konfirm($id)
  354. {
  355. $cek = $this->m_all->cek($id, 'brand', 'tb_itemofbasic');
  356. if ($cek->num_rows() > 0)
  357. {
  358. foreach ($cek->result() as $row)
  359. {
  360. $aktif = $row->aktif;
  361. }
  362. }
  363. else $aktif = 0;
  364.  
  365. if ($aktif == 0)
  366. {
  367. $this->m_all->hapus($id, '', $this->primary, '',$this->table);
  368.  
  369. ob_start();
  370. $html = ob_get_contents();
  371. ob_end_clean();
  372.  
  373. $response = array('status'=>'true', 'pesan'=> 'Data berhasil di-hapus','html'=>$html);
  374. }
  375. else $response = array('status'=>'false','pesan'=>'Data product atas brand, aktif');
  376.  
  377. echo json_encode($response);
  378. }
  379.  
  380. function _get_nama($lvl)
  381. {
  382. $get_nm_lvl = $this->m_all->getNama('level', $lvl, null, 'level_nama', 'id_level');
  383. if ($get_nm_lvl->num_rows() > 0)
  384. {
  385. foreach ($get_nm_lvl->result() as $gnl)
  386. {
  387. $p = $gnl->level_nama;
  388. }
  389. }
  390. else $p = '';
  391. return $p;
  392. }
  393.  
  394.  
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement