Tutorial Codeigniter: Menampilkan Data dari Database Menggunakan Codeigniter 3

Tutorial Codeigniter: Menampilkan Data dari Database Menggunakan Codeigniter 3

16 Juni 2020 nefrit Codeigniter

Tutorial kali ini akan membahas tentang bagaimana cara menampilkan data dari database sehingga dapat tampilkan pada halaman website.

Silahkan buat database dengan nama tutorial_codeigniter_3

CREATE DATABASE tutorial_codeigniter_3;

Selanjutnya buat table dengan nama employee_tbl. Untuk cara simple nya silahkan exec query sql dibawah:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE TABLE `employee_tbl` (
  `id` int(11) NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `alamat` varchar(100) NOT NULL,
  `divisi` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Kemudian jalankan sql query dibawah ini kita akan menambahkankan 1 data record dalam database, karena data ini yang akan kita tampilkan nanti pada view.

INSERT INTO `employee_tbl` (`id`, `full_name`, `alamat`, `divisi`) VALUES
(1, 'Nefrit ID', 'Sario Kota Baru, Kota Manado, Prov. Sulawesi Utara', 'IT');
ALTER TABLE `employee_tbl`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `employee_tbl`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

Kemudian pastikan configurasi database.php pada folder "application\config\database.php" sudah sesuai dengan nama database, username & password database.
Jika menggunakan xampp dengan configurasi default maka isi config database.php bisa disamakan dengan config dibawah ini:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'tutorial_codeigniter_3',
'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
);


Buat sebuah model pada codeigniter dengan nama Employee_m.php dan masukan script dibawah ini:


<?php 
class Employee_m extends CI_Model{
    function tampilkan_employee(){
        return $query = $this->$db->query("SELECT * FROM `employee_tbl`");
    }
}

Buat sebuah controller dengan nama Employee.php yang akan memanggil function dari model Employee_m yang telah kita buat barusan dan masukan script seperti berikut pada controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->model('employee_m');
    }
    public function index(){
        $data['employee'] = $this->employee_m->get_employee();
        $this->load->view('employee', $data);
        
    }
}

Selanjut kita tinggal membuat sebiah view untuk menampilkan data dari database. Buat view dengan nama employee.php dan berikan script dibawah ini:

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home | Manado Coder</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="container mt-2">
    <section>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="#">Manado Coder</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item ">
                        <a class="nav-link" href="#">Home </a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Employee </span></a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Dropdown
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </li>
  
                </ul>
                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                </form>
            </div>
        </nav>
    </section>
    <!-- Tabel -->
    <table class="table mt-3">
        <thead class="thead-light">
            <tr>
                <th scope="col">Nama</th>
                <th scope="col">Alamat</th>
                <th scope="col">Divisi</th>
            </tr>
        </thead>
        <tbody>
            <tr>
            <?php foreach($employee->result() as $row): ?>
                <td><?= $row->full_name ?></td>
                <td><?= $row->alamat ?></td>
                <td><?= $row->divisi ?></td>
            <?php endforeach; ?>
            </tr>
        </tbody>
    </table>
    <!-- Footer -->
    <footer class="page-footer  bg-dark mt-5">
        <div class="footer-copyright text-center py-3"><span class="text-white">© 2020 Copyright:</span>
            <a href="https://manadocoder.com" class="text-white"> manadocoder.com</a>
        </div>
    </footer>
</div>
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
Silahkan buka http://localhost/codeigniter/index.php/employee pada browser anda dan hasilnya akan seperti pada gambar featured image paling atas.


Back to blog list

Tags

Bagikan


Join Discussion

Copyright 2020 © manadocoder.com. All rights reserved.
    Page rendered in 0.0747 seconds.