Web Programming : Meet 5 (Menampilkan Data pada Database)

Meet 5: Menampilkan isi database : download disini

Setelah sebelumnya di pertemuan ke 4 (meet 4) kita telah membahas tentang konfigurasi codeigniter dan menampilkan file view di controllers, selanjutnya yang dibahas adalah menampilkan isi dari tabel dengan menggunakan Models yang akan di panggil oleh controllers dan ditampilkan di view

Buat sebuah database (database juga bisa didownload di materi sebelumnya disini) .

Dump Query ini di SQL (Phpmyadmin)

-- PHP Version: 5.6.33

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `db_mhs`
--

-- --------------------------------------------------------

--
-- Table structure for table `data_mhs`
--

CREATE TABLE `data_mhs` (
  `id` int(11) NOT NULL,
  `nim` varchar(12) NOT NULL,
  `nama` varchar(100) NOT NULL,
  `jurusan` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `data_mhs`
--

INSERT INTO `data_mhs` (`id`, `nim`, `nama`, `jurusan`) VALUES
(1, '168410040021', 'Agnes Wiranda', 'Informatics Komputer'),
(2, '168410040028', 'Agus Rohmatullah A', 'Informatics Komputer'),
(3, '168410040001', 'Ahmad Fauzi', 'Informatics Komputer'),
(4, '168410040023', 'Ahmad Krisdianto', 'Informatics Komputer'),
(5, '168410040008', 'Eni Maryanti', 'Informatics Komputer');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `data_mhs`
--
ALTER TABLE `data_mhs`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `nim` (`nim`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `data_mhs`
--
ALTER TABLE `data_mhs`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;

/*!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 */;

atau save as .sql kemudian import

Selanjutnya di folder Controllers dibuat seperti ini (edit file sebelumnya)

Mahasiswa.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Mahasiswa extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('Mhs_model');
    }

  public function index()
  {
    $data = array(
          'judul'	=> 'Daftar Nama Mahasiswa',	
          'mhs'	=> $this->Mhs_model->data(),
        );

    $this->load->view('data_mhs',$data);	
  }

}

Kemudian di file data_mhs.php di folder views (application/views)

<!doctype html>
<html>
  <head>
    <title>Data Mahasiswa</title>
  </head>
<body>
  <table width="100%" border="1">
    <tr>
      <td>No</td>
      <td>NIM</td>
      <td>Nama</td>
      <td>Jurusan</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1234567</td>
      <td>Agnes W</td>
      <td>Informatics Computer</td>
    </tr>
    <tr>
      <td>2</td>
      <td>1234567</td>
      <td>Agus R</td>
      <td>Informatics Computer</td>
    </tr>
    <tr>
      <td>3</td>
      <td>1234567</td>
      <td>Ahmad Fauzi</td>
      <td>Informatics Computer</td>
    </tr>
    <tr>
      <td>4</td>
      <td>1234567</td>
      <td>Ahmad Krisdianto</td>
      <td>Informatics Computer</td>
    </tr>
    <tr>
      <td>5</td>
      <td>1234567</td>
      <td>Eni Maryanti</td>
      <td>Informatics Computer</td>
    </tr>
  </table>
  <h1>### CONTOH MENAMPILKAN DATA ###</h1><hr>
  <h1><?php echo $judul;?></h1>
  <table width="100%" border="1">
    <tr>
      <th>No</th>
      <th>NIM</th>
      <th>Nama</th>
      <th>Jurusan</th>
    </tr>
    <?php 
      $nomor = 1;
      foreach($mhs as $mhs){ ?>
    <tr>
      <td><?php echo $nomor++;?></td>
      <td><?php echo $mhs->nim;?></td>
      <td><?php echo $mhs->nama;?></td>
      <td><?php echo $mhs->jurusan;?></td>
    </tr>
    <? } ?>
  </table>
</body>
</html>

 

Recommended For You

About the Author: adyrhs

Leave a Reply

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