Gambar 1. Select Database -CRUD 1 -Codeigniter3 |
Source SQL:
-- -------------------------------------------------------- -- -- Struktur dari tabel `user` -- CREATE TABLE IF NOT EXISTS `user` ( `role` int(1) NOT NULL, `id_user` int(4) NOT NULL, `username` varchar(50) NOT NULL, `password` char(128) NOT NULL, `last_login` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data untuk tabel `user` -- INSERT INTO `user` (`role`, `id_user`, `username`, `password`, `last_login`) VALUES (1, 1, 'hasta', 'C1CF6A5C0E168B080C446E7F5600B2404BA06387DC10D96014C941E5F82615AE96AA231727D38728AA446F26B6765CFDBA3E9A18F27E39B0A4B8EA2A541B4E41', '2016-08-14 00:00:00'), (2, 2, 'andi', 'ED0D587073B2A487FA0638D970255179F0F4D298B33ED39317797681BB57E2277C560FFB9A3F75A81ADC261D4D7CEE06769380751D44E0669226D4CF042E44B0', '2016-08-14 00:00:00'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
1. atur database:
hostname: localhost
usernam:root
password:
database:latihanci
(Pengaturan database disesuaikan dengan database yang akan digunakan)
source code: database.php (...\application\config\database.php)
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'latihanci', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
2. Panggil Library database menjadi autoload
source code: database.php (...\application\config\autoload.php)
$autoload['libraries'] = array('database');
3.Ubah default_controller agar control Login.php (nama controller yang akan kita buat) menjadi halaman depan/utama :
source code: database.php (...\application\config\autoload.php)
$route['default_controller'] = 'login';
4.
source code: Login.php (...\application\controllers\Login.php)
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller{ function __construct(){ parent::__construct(); $this->load->model('data_user'); $this->load->helper('url'); } public function index(){ $data['user']=$this->data_user->tampil_data()->result(); $this->load->view('loginform',$data); } }
5.
source code: data_user.php (...\application\models\data_user.php)
<?php class data_user extends CI_Model{ function tampil_data(){ return $this->db->get('user'); } }
6.
source code: loginform.php(...\application\views\loginform.php)
<!DOCTYPE html> <html> <head> <title>Select Database CI3</title> </head> <body> <center><h1>Codeigniter 3</h1></center> <table style="margin:20px auto;" border="1"> <tr> <th>No</th> <th>Role</th> <th>Id User</th> <th>Username</th> <th>Password</th> <th>Last Login</th> </tr> <?php $no = 1; foreach($user as $u){ ?> <tr> <td><?php echo $no++ ?></td> <td><?php echo $u->role ?></td> <td><?php echo $u->id_user ?></td> <td><?php echo $u->username ?></td> <td><?php echo $u->password ?></td> <td><?php echo $u->last_login ?></td> </tr> <?php } ?> </table> </body> </html>
0 komentar:
Post a Comment