Hallo balik lagi ditutorial pemrograman Dumet School, karena pada artikel sebelumnya kita sudah membahas tentang cara configurasi dari codeigniter dan membuat database beserta tablenya, nah pada bagian ke 2 ini kita akan membuat sebuah controller, oke coba teman teman buat file
baru dengan nama TestMatch.php pada folder controller, coba tambahkan script berikut :
|
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 54 55 56 57 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class TestMatch extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library(['form_validation','email','session']); $this->load->helper(['form','security']); } public function index() { $this->load->view('users/registerUser'); } public function register() { $this->form_validation->set_rules('fullname', 'Fullname', 'required|trim|xss_clean|min_length[3]|max_length[25]'); $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|is_unique[users.email]|min_length[11]|max_length[40]|valid_email'); $this->form_validation->set_rules('handphone', 'Handphone', 'required|trim|xss_clean|is_unique[users.handphone]|min_length[8]|max_length[12]'); $this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|min_length[8]|max_length[12]'); if ($this->form_validation->run() == false ) { $this->load->view('users/registerUser'); } else { $this->load->library(['encryption']); $this->load->helper(['string']); $fullname = $this->input->post('fullname', true); $email = $this->input->post('email', true); $handphone = $this->input->post('handphone', true); $password = $this->input->post('password', true); $this->db->insert('users', [ 'fullname' => $fullname, 'email' => $email, 'handphone' => $handphone, 'password' => password_hash($password, PASSWORD_BCRYPT), 'status' => 0 ]); $user_id = $this->db->insert_id(); $activation_code = $user_id.random_string('alnum', 128); $this->db->update('users', ['activation_code' => $activation_code], ['id' => $user_id]); redirect('user/registerUser'); } } } |
Oke pada tahap ke 2 ini kita cukupkan sampai pembuatan controller dan pada ketahap ke 3 nanti kita akan fokus di view untuk menampilkan datanya yah yang pasti bakal lebih keren, oke cukup sekian tutorial tentang Bagaimana Cara Membuat Unique Validation Menggunakan CodeIgniter PART II. Sampai bertemu diartikel selanjutnya dan sampai jumpa.
#KeepLearn
#IsmetMA