Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controllers;
- use CodeIgniter\Controller;
- use App\Models\JurusanModel;
- class Jurusan extends Controller
- {
- public function index()
- {
- $model = new JurusanModel();
- $data['jurusan'] = $model->orderBy('id', 'ASC')->findAll();
- return view('/jurusan/index', $data);
- }
- public function tambah()
- {
- $data['validation'] = '';
- return view('/jurusan/tambah', $data);
- }
- public function simpan()
- {
- helper(['form', 'url']);
- // $val = $this->validate([
- // 'jurusan' => 'required|string',
- // 'kapasitas' => 'integer',
- // 'terisi' => 'integer|less_than_equal_to[' . $this->request->getVar('kapasitas') . ']'
- // ]);
- // $model = new JurusanModel();
- // $data['validation'] = $this->validator;
- // if (!$val) {
- // return view('/jurusan/tambah', $data);
- // } else {
- // $model->save([
- // 'jurusan' => $this->request->getVar('jurusan'),
- // 'kapasitas' => $this->request->getVar('kapasitas'),
- // 'terisi' => $this->request->getVar('terisi')
- // ]);
- // return redirect()->to(base_url('/jurusan'));
- // }
- // CARA <KE-2></KE-2>
- $validation = \Config\Services::validation();
- $data = array(
- 'jurusan' => $this->request->getPost('jurusan'),
- 'kapasitas' => $this->request->getPost('kapasitas'),
- 'terisi' => $this->request->getPost('terisi')
- );
- if ($validation->run($data, 'jurusan') == FALSE) {
- session()->setFlashdata('inputs', $this->request->getPost());
- session()->setFlashdata('errors', $validation->getErrors());
- return redirect()->to(base_url('jurusan/tambah'));
- } else {
- $model = new JurusanModel();
- $simpan = $model->insert($data);
- if ($simpan) {
- session()->setFlashdata('success', 'Berhasil menyimpan Jurusan ' . $this->request->getPost('jurusan'));
- return redirect()->to(base_url('jurusan'));
- }
- }
- }
- public function edit($id)
- {
- $model = new JurusanModel();
- $data['jurusan'] = $model->where('id', $id)->first();
- return view('/jurusan/edit', $data);
- }
- public function update()
- {
- $id = $this->request->getPost('id');
- $validation = \Config\Services::validation();
- $data = array(
- 'jurusan' => $this->request->getPost('jurusan'),
- 'kapasitas' => $this->request->getPost('kapasitas'),
- 'terisi' => $this->request->getPost('terisi')
- );
- if ($validation->run($data, 'jurusan') == FALSE) {
- session()->setFlashdata('inputs', $this->request->getPost());
- session()->setFlashdata('errors', $validation->getErrors());
- return redirect()->to(base_url('jurusan/edit/' . $id));
- } else {
- $model = new JurusanModel();
- $update = $model->update($id, $data);
- if ($update) {
- session()->setFlashdata('success', 'Berhasil mengupdate Jurusan ' . $this->request->getPost('jurusan'));
- return redirect()->to(base_url('jurusan'));
- }
- }
- }
- public function hapus($id)
- {
- $model = new JurusanModel();
- $hapus = $model->where('id', $id)->delete();
- if($hapus) {
- session()->setFlashdata('success', 'Jurusan Berhasil dihapus');
- return redirect()->to(base_url('jurusan'));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement