Belajar Membuat Website dengan CI (bag3)

MySQL Database merupakan basis data untuk menyimpan data sebuah website. Pada framework CodeIgniter juga bisa dikoneksikan dengan sebuah database. Dalam belajar aplikasi kali ini akan membahas bagaimana cara mengkoneksikan MySQL Database dengan Framework CI.

MySQL Database CI
MySQL Database CI

Masuk ke project toko_online.
– Buka folder “application
– Buka folder “config
– Buka file “database.php
– Lengkapi coding seperti berikut :

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'dbtoko_online',
'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
);

nb :
– hostname => ‘localhost’,
– username => ‘root’,
– password => ”,
– database => ‘dbtoko_online’

– Buka folder application/config
– Buka file autoload.php
– Lengkapi coding seperti berikut :

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

– Buka folder application/config
– Buka file routes.php

– Lengkapi coding seperti berikut :

$route['default_controller'] = 'home';

– Buka folder application/config
– Buka file config.php

– Lengkapi coding seperti berikut :

$config['base_url'] = 'http://localhost/toko_online/';

Kita akan menampilkan data produk. Langkah yang dilakukan yaitu mengisi database yang sudah dibuat pada tabel barang.
– Buka browser ketikkan pada url “http://localhost/phpmyadmin
– Pilih database “dbtoko_online
– Pilih tabel “barang
– Klik tab menu SQL
– Isi query seperti berikut:

INSERT INTO `barang` (`id_barang`, `nama_barang`, `harga`, `stok`) VALUES
(1, 'XIAOMI Redmi 5A Gold', 999000, 10),
(2, 'iPhone 5s Silver 32Gb', 2200000, 5),
(3, 'Samsung Galaxy 7 128gb', 5400000, 3),
(4, 'Vivo V9 LTE 256Gb', 2500000, 2);

Karena CodeIgniter menggunakan MVC selanjutnya kita akan membuat Model terlebih dahulu.
– buka folder “application
– buka folder “models
– buat file “Md_produk.php
– ketikkan code:

<?php Class Md_produk extends CI_Model { function get_produk() { $query = $this->db->get('barang');
      return $query->result();
   }
}

Masuk ke folder “Controller“.
– buat file “Home.php
– ketikkan coding berikut:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { public function index() { $this->load->view('beranda');
   }

   public function beranda()
   {
      $data['title'] = 'Beranda';
      $this->load->view('beranda',$data);
   }

   public function produk()
   {
      $data['title'] = 'Produk';
      $this->load->model('md_produk');
      $data['query'] = $this->md_produk->get_produk();
      $this->load->view('produk',$data);
   }
}

Masuk ke folder “View“.
– Buat file “produk.php
– Ketikkan coding berikut:

<!DOCTYPE html>
<html lang="en">
   <?php require_once 'req_header.php';?>
<body>
   <?php require_once 'req_header_section.php';?>
   <?php require_once 'req_navigation_bar.php';?>
   <!-- Body Section -->
 
<div class="row">
  
<div id="sidebar" class="span3">
   <?php require_once 'req_kategori_bar.php';?>

<div class="well well-small alert alert-warning cntr">

<h2>50% Discount</h2>
only valid for online order. 

<a class="defaultBtn" href="#">Click here </a>
 
   </div>

<div class="well well-small" ><a href="#"><img src="<?php echo base_url();?>assets/img/paypal.jpg" alt="payment method paypal"></a></div>

   <a class="shopBtn btn-block" href="#">Upcoming products     
<small>Click to view</small></a>
   
<ul class="nav nav-list promowrapper">
<li>
<div class="thumbnail">
   <a class="zoomTool" href="product_details.html" title="add to cart"><span class="icon-search"></span> QUICK VIEW</a>
   <img src="<?php echo base_url();?>assets/img/b.jpg" alt="bootstrap ecommerce templates">
 
<div class="caption">
<h4><a class="defaultBtn" href="product_details.html">VIEW</a> <span class="pull-right">Rp 120.000,-</span></h4>
   </div>
   </div>
   </li>


<li style="border:0"> &nbsp;</li>
<li>
<div class="thumbnail">
   <a class="zoomTool" href="product_details.html" title="add to cart"><span class="icon-search"></span> QUICK VIEW</a>
   <img src="<?php echo base_url();?>assets/img/b.jpg" alt="shopping cart template">
  
<div class="caption">
<h4><a class="defaultBtn" href="product_details.html">VIEW</a> <span class="pull-right">Rp 120.000,-</span></h4>
   </div>
   </div>
   </li>
<li style="border:0"> &nbsp;</li>
<li>
<div class="thumbnail">
   <a class="zoomTool" href="product_details.html" title="add to cart"><span class="icon-search"></span> QUICK VIEW</a>
   <img src="<?php echo base_url();?>assets/img/b.jpg" alt="bootstrap template">
   
<div class="caption">
<h4><a class="defaultBtn" href="product_details.html">VIEW</a> <span class="pull-right">Rp 120.000,-</span></h4>
   </div>
   </div>
   </li>
   </ul>
   </div>

<div class="span9">
   <!-- New Products -->
 
<div class="well well-small">
   <h3>Produk Kami </h3>
   <?php
      $slice = 1;
      foreach ($query as $row)
      {
         if ($slice==4){
            $slice=1;
         }

         if ($slice==1){
            echo '

<div class="row-fluid">
';
            echo '

<li class="span4">


<div class="thumbnail">
                  <a href="product_details.html" class="overlay"></a>
                  <a class="zoomTool" href="product_details.html" title="add to cart"><span class="icon-search"></span> QUICK VIEW</a>
                  <a href="product_details.html"><img src="'.base_url().'assets/img/b.jpg" alt=""></a>     

<div class="caption cntr">
                  
'.$row->nama_barang.'

<strong> Rp '.number_format($row->harga,0,',','.').',-   </strong>

<h4><a class="shopBtn" href="#" title="add to cart"> Add to cart </a></h4>

<div class="actionList">
                  <a class="pull-left" href="#">Add to Wish List </a>
                  <a class="pull-left" href="#"> Add to Compare </a>
                 </div>
                 <br class="clr">
                 </div>
                 </div>
                 </li>
';
         }

         if ($slice != 1) {
            echo '

<li class="span4">
<div class="thumbnail">
                  <a href="product_details.html" class="overlay"></a>
                  <a class="zoomTool" href="product_details.html" title="add to cart"><span class="icon-search"></span> QUICK VIEW</a>
                  <a href="product_details.html"><img src="'.base_url().'assets/img/b.jpg" alt=""></a>
                  
<div class="caption cntr">
                  
'.$row->nama_barang.'            

<strong> Rp '.number_format($row->harga,0,',','.').',-   </strong>
<h4><a class="shopBtn" href="#" title="add to cart"> Add to cart </a></h4>
<div class="actionList">
                  <a class="pull-left" href="#">Add to Wish List </a>
                  <a class="pull-left" href="#"> Add to Compare </a>
                  </div>
                  <br class="clr">
                  </div>
                  </div>
                  </li>


';
         }

         if ($slice == 3) {
            echo '</div>
';
         }

         $slice++;
      }
   ?>
   </div>
   </div>
   <!-- Clients -->
  
<section class="our_client">
<hr class="soften"/>
<h4 class="title cntr"><span class="text">Sponsor by</span>.  </h4>
<hr class="soften"/>
<div class="row">

<div class="span2">
   <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/1.png"></a>
  </div>

<div class="span2">
   <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/2.png"></a>
   </div>

<div class="span2">
   <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/3.png"></a>
   </div>

<div class="span2">
  <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/4.png"></a>
   </div>

<div class="span2">
   <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/5.png"></a>
   </div>

<div class="span2">
   <a href="#"><img alt="" src="<?php echo base_url();?>assets/img/6.png"></a>
   </div>
   </div>
   </section>
   <?php require_once 'req_footer.php';?>
</body>
</html>

Buka browser ketikkan url “http://localhost/toko_online“.
Tampilan seperti berikut :

Tampilan Hasil Koneksi Database Menggunakan CI
Tampilan Hasil Koneksi Database Menggunakan CI

DEMO TOKO ONLINE

Selamat mencoba, Happy Coding 🙂

8 Comments

  1. gan ini maksudnya buat file baru home.php atau hanya ditambahkan codingnya saja?
    karna kan sebelumnya sudah ada file home.

    1. Itu buat file baru di Controller “Home.php”. Defaultnya memang sudah ada Welcome.php. Sebelumnya masuk ke folder application/config buka file “routes.php” ubah coding seperti ini $route[‘default_controller’] = ‘home’; .
      Terima kasih sudah berkunjung. 🙂

  2. maaf tapi disaya masih muncul notice:

    Message: Use of undefined constant database – assumed ‘database’

    Filename: config/autoload.php

    Line Number: 61

    Backtrace:

    File: C:\xampp\htdocs\toko_online\application\config\autoload.php
    Line: 61
    Function: _error_handler

    File: C:\xampp\htdocs\toko_online\index.php
    Line: 315
    Function: require_once

    bagaimana cara menyelesaikannya?

  3. Tolong bantuannya gan, punya saya error. Unable to load the requested file: beranda.php

Leave a Reply

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