• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Jaranguda

Belajar Mengajar

  • Home
  • Sponsor/Jasa
  • Tentang

Menampilkan Data Dari Database CodeIgniter 4

Last Updated on 26 February 2020 By tommy Leave a Comment

Tutorial kali ini kita akan membahas cara mudah menampilkan data dari database MySQL ke dalam tabel dengan Bootstrap 4. Ada 5 tahap ditutorial ini.

Konfigurasi Database

Buka file .env, ubah bagian database menjadi

database.default.hostname = localhost
database.default.database = tutorial
database.default.username = root
database.default.password = VHMAxc7Wx7b3crUhx4rH
database.default.DBDriver = MySQLi

Import Database

Import database contoh, untuk memudahkan membuat contoh ini

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  `country` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `password`, `country`) VALUES
(1, 'Winston', 'Bebis', '[email protected]', 'RCOWs2uJq7', 'Indonesia'),
(2, 'Ethelyn', 'Henrys', '[email protected]', 'ZwirrolHMukN', 'Russia'),
(3, 'Vittorio', 'Vellender', '[email protected]', 'hz7uqNpZ', 'Brazil'),
(4, 'Gearalt', 'Scinelli', '[email protected]', 'noer33vU', 'Poland'),
(5, 'Fey', 'Stevens', '[email protected]', 'j1afGfuw7Ve7', 'China'),
(6, 'Bentley', 'Ditt', '[email protected]', 'BUR6fv0A', 'Portugal'),
(7, 'Tonya', 'Rye', '[email protected]', 'X5tmZ9jDcS', 'China'),
(8, 'Juline', 'Roughey', '[email protected]', 'kjRRO45jeD0w', 'Indonesia'),
(9, 'Alic', 'Wevell', '[email protected]', 'TzSWEfZWy8vQ', 'China'),
(10, 'Norbie', 'Gelder', '[email protected]', '4jYZi2rj', 'Albania'),
(11, 'Emmalynn', 'Gabler', '[email protected]', 'Uri553LlQO', 'Sweden'),
(12, 'Sibelle', 'Bernardo', '[email protected]', 'Lbiznun', 'Ukraine'),
(13, 'Laurie', 'Spooner', '[email protected]', 'IpkvoEL96BXc', 'China'),
(14, 'Othilie', 'Eckly', '[email protected]', '27kZgqGFJMmL', 'Venezuela'),
(15, 'Artus', 'Pennycook', '[email protected]', 'QOs7uRh5yLKB', 'Mali'),
(16, 'Vincenz', 'Osler', '[email protected]', 'Gu5QCXHQ', 'China'),
(17, 'Isidro', 'Hemphall', '[email protected]', 'NQ4KEqzc', 'France'),
(18, 'Pinchas', 'Le Guin', '[email protected]', 'ZsfzyYqo', 'China'),
(19, 'Rorie', 'Aleksandrev', '[email protected]', 'dstH8RB5', 'South Africa'),
(20, 'Mayer', 'Pitkethly', '[email protected]', 'OHjZtbmX5gt', 'Canada'),
(21, 'Karol', 'Baiden', '[email protected]', 'EZ1iIWBYV1p6', 'Morocco'),
(22, 'Franciska', 'Peeters', '[email protected]', 'QJX2b8DfGs', 'Russia'),
(23, 'Chlo', 'Kennford', '[email protected]', 'NU8YjWQF', 'Portugal'),
(24, 'Wolfie', 'Lownds', '[email protected]', '1D00dQaOaO', 'Greece'),
(25, 'Sherri', 'Hellens', '[email protected]', 'Ee7nls3', 'Poland'),
(26, 'Emory', 'Semark', '[email protected]', 'VAIBfR8uO', 'Tajikistan'),
(27, 'Jakie', 'Hulks', '[email protected]', 'jI5ylhi', 'China'),
(28, 'Leticia', 'Mathie', '[email protected]', 'euFzcmE', 'Indonesia'),
(29, 'Raimondo', 'Milier', '[email protected]', '3fNmX69MpoiF', 'Poland'),
(30, 'Fawn', 'Cush', '[email protected]', 'odFVlovDiSj9', 'China');
 
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);
 
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=51;
COMMIT;

Membuat Model

Buat model app/Models/UserModel.php untuk memanggil data dari database

<?php namespace App\Models;
 
use CodeIgniter\Model;
 
class UserModel extends Model
{
    protected $table = 'users';
 
    public function getUsers()
    {
        return $this->findAll();
    }
 
}

Membuat Controller

Buat controller app/Controllers/User.php, controller ini nanti yang digunakan untuk memanggil url, juga untuk mengarahkan kemana view yang ditampilkan.

<?php namespace App\Controllers;
 
use App\Models\UserModel;
 
class User extends BaseController
{
    public function index()
    {
        $users = new UserModel();
        $data = $users->getUsers();
        return view('user', compact('data'));
    }
}

Membuat View

Buat view baru app/Views/user.php, disini data akan kita tampilkan ke enduser.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="jaranguda.com tutorial codeigniter4">
    <title>Codeigniter 4 - Jaranguda.com</title>
 
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <style>
body {
  padding-top: 5rem;
}
 
    </style>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
 
  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="dropdown01">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>
 
<main role="main" class="container-fluid">
 
    <h1>Tutorial Codeigniter 4</h1>
    <hr>
 
 
 
 
<table class="table">
  <tt>
    <th>Name</th>
    <th>Email</th>
    <th>Password</th>
  </tr>
 
<?php
foreach ($data as $user) {
  // var_dump($user);
?>
  <tr>
    <td><?php echo $user['first_name'] . ' ' .  $user['last_name']; ?></td>
    <td><?php echo $user['email']; ?></td>
    <td><?php echo $user['password']; ?></td>
  </tr>
<?php
}
?>
</table>
 
</main><!-- /.container -->
</body>
</html>

Jalankan development server CodeIgniter

php spark serve

buka alamat http://localhost:8000 di browser
tampilan di browser codeigniter

Last Updated on 26 February 2020 by tommy

Tulisan menarik lainnya

  • Menampilkan Data Dari Database MySQL ke Combobox dengan PHP

    Pada tutorial kali ini, kita akan menampilkan data dari database kedalam combobox. Buat sebuah database…

  • Menampilkan Data Akun Reseller Reseller.co.id

    Salah satu registrar domain Indonesia (reseller.co.id) belum memiliki API untuk mengelola domain-domain yang kita miliki,…

  • Mengambil data Kurs dari Situs BRI

    Situs Bank Rakyat Indonesia baru saja mengupdate tampilan web dan struktur tabel kurs nya, sehingga…

  • Mengambil Data Jadwal Penerbangan dari Situs Batavia

    Jadwal yang akan kita ambil adalah jadwal penerbangan pesawat Batavia langsung dari websitenya, jadi apabila…

Filed Under: PHP

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Pencarian

Tanya Jawab tentang DevOps SRE CPE, gabung di https://t.me/devopsindonesia

Terbaru

  • Cara Mengganti Port Screen Sharing macOS
  • Cara Menonaktifkan Pager di macOS
  • Cara Mengupdate Nama Apple silicon-as-a-Service Scaleway
  • Cara Force Delete Namespace di Kubernetes
  • Install PHP MariaDB di Mac Monterey

Komentar

  • mazda on Tutorial Lengkap Install Mail Server Postfix Dovecot MariaDB di CentOS 7
  • adi on Menggunakan Mikrotik Sebagai SSH Client
  • aris u on Solusi Simple Queue Mikrotik Tidak Berjalan
  • Bowo on Cara Mematikan SSID Molecool Balifiber
  • aris on Solusi Simple Queue Mikrotik Tidak Berjalan

Tulisan Populer

  • Password Router Huawei HG8245H5 Indihome 1.2m views
  • Password Terbaru ZTE F609 Indihome 784.5k views
  • Password Superadmin Huawei HG8245A 318.7k views
  • Cara Setting Manual Modem GPON ZTE F609 Indihome 273.1k views
  • Cara Setting Wireless ZTE F609 Indihome 257.2k views
  • Mengaktifkan Port LAN di Huawei HG8245 Indihome 169.9k views
  • Akses UseeTV Indihome via Wireless ZTE F609 156.8k views
  • Kemana Menghilangnya Saldo BCA 50 ribu 153.4k views
  • Cara Reset Password ZTE F609 Indihome 147.6k views
  • Cara Setting DHCP Server Modem/Router ZTE F609 113.6k views

Kategori

  • Delphi
  • dll
  • Gambas
  • Internet
  • Java
  • Lazarus
  • Linux
  • PHP
  • Review
  • Teknologi

Sponsor

kadal.id
carakami.com
kuotabisa.com
Untuk jadi sponsor, hubungi kita lewat halaman sponsor
© 2021. Jaranguda
  • Linux
  • PHP
  • Internet
  • Teknologi
  • Delphi
  • Gambas
  • Java