Membuat Portal Berita Dengan CodeIgniter dan Bootstrap

Download file CodeIgniter dan Boostrap yang sudah disiapkan dan di-setting dibawah ini

Project

Buat Database dengan Nama “project”  kemudian import query ini kedalamnya

-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: May 15, 2018 at 08:12 AM
-- Server version: 10.1.30-MariaDB
-- 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: `project`
--

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

--
-- Table structure for table `admin`
--

CREATE TABLE `admin` (
  `id` int(2) NOT NULL,
  `username` varchar(15) NOT NULL,
  `password` varchar(75) NOT NULL,
  `nama` varchar(15) NOT NULL,
  `level` int(1) NOT NULL,
  `status` int(1) NOT NULL,
  `foto` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `admin`
--

INSERT INTO `admin` (`id`, `username`, `password`, `nama`, `level`, `status`, `foto`) VALUES
(1, 'supervisor', 'e10adc3949ba59abbe56e057f20f883e', 'supervisor', 1, 1, ''),
(2, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 'admin aja', 2, 1, ''),
(3, 'user', 'e10adc3949ba59abbe56e057f20f883e', 'user biasa', 3, 1, '');

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

--
-- Table structure for table `tb_artikel`
--

CREATE TABLE `tb_artikel` (
  `id` int(10) NOT NULL,
  `judul` varchar(100) NOT NULL,
  `isi` text NOT NULL,
  `gbr` varchar(30) NOT NULL,
  `id_kategori` int(2) NOT NULL,
  `tgl_terbit` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Table structure for table `tb_kategori`
--

CREATE TABLE `tb_kategori` (
  `id_kategori` int(2) NOT NULL,
  `nama_kategori` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `admin`
--
ALTER TABLE `admin`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `tb_artikel`
--
ALTER TABLE `tb_artikel`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `tb_kategori`
--
ALTER TABLE `tb_kategori`
  ADD PRIMARY KEY (`id_kategori`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `admin`
--
ALTER TABLE `admin`
  MODIFY `id` int(2) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `tb_artikel`
--
ALTER TABLE `tb_artikel`
  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `tb_kategori`
--
ALTER TABLE `tb_kategori`
  MODIFY `id_kategori` int(2) NOT NULL AUTO_INCREMENT;
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 */;

Home_model.php

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

class Home_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    function semua_kategori(){
    	return $this->db->query("SELECT * FROM tb_kategori")->result();
    }

    function total_rows($q = NULL) {
        $this->db->order_by('id', 'ASC');
        $this->db->select('a.*,b.*');
        $this->db->from('tb_artikel a');
        $this->db->join('tb_kategori b','a.id_kategori=b.id_kategori','LEFT');
        $this->db->like('judul', $q);
    $this->db->or_like('isi', $q);
    $this->db->or_like('nama_kategori', $q);
        return $this->db->count_all_results();
    }

    function get_limit_data($limit, $start = 0, $q = NULL) {
        $this->db->order_by('id', 'ASC');
        $this->db->select('a.*,b.*');
        $this->db->from('tb_artikel a');
        $this->db->join('tb_kategori b','a.id_kategori=b.id_kategori','LEFT');
        $this->db->like('judul', $q);
    $this->db->or_like('isi', $q);
    $this->db->or_like('nama_kategori', $q);
    $this->db->limit($limit, $start);
          return $this->db->get()->result();
    }

}

Controller Home.php

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

class Home extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Home_model');
    }

    function index(){

        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'home/index?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'home/index?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'home/index';
            $config['first_url'] = base_url() . 'home/index';
        }
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Home_model->total_rows($q);
        $data_home = $this->Home_model->get_limit_data($config['per_page'], $start, $q);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        
        $data = array(
            'data_home' => $data_home,
            'kategori'  => $this->Home_model->semua_kategori(),         	
        );
        $this->load->view('home/home',$data);

    }

}

View home.php

<html>
 <head>
  <title>Project Tugas Besar</title>
  <link rel="stylesheet" href="<?php echo base_url('bootstrap/css/bootstrap.min.css') ?>">
 </head>
 <body>
 <!-- kontainer -->
 <div class="container">
 	<div class="row">
 		<div class="col-md-12"> <!-- menu -->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">PROJECT</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="#"><i class="glyphicon glyphicon-home"></i> HOME</a></li>
        <li><a href="#"> ARTICLE</a></li>
        <li><a href="#"> PROFILE</a></li>
      </ul>
      
      <ul class="nav navbar-nav navbar-right">
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i> Cari</button>
      </form>
  	  </ul>
      
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
 		</div> <!-- /menu -->
 		<div class="col-md-12"> <!-- header gambar -->
 			<img src="<?php echo base_url('gambar/gbr_1522902075.jpg');?>" style="width: 100%;height: 100px;">
 		</div> <!-- /header gambar -->
 		<div class="col-md-12"> <!-- Isi dan Sidebar -->
 			<div class="row">
 				<div class="col-md-8"> <!-- artikel -->
          <?php foreach($data_home as $hd) {;?>
            <div class="row">
              <div class="col-md-3">
                <?php if(!empty($hd->gbr)) {?>
                  <img src="<?php echo base_url('gambar/'.$hd->gbr)?>" class="img img-responsive" style="max-width: 100px;">
                <?php }else{?>
                  <img src="<?php echo base_url('gambar/no-image.jpg')?>" class="img img-responsive" style="max-width: 100px;">
                <?php } ?>
              </div>
              <div class="col-md-9">
                <?php echo $hd->judul;?> - <?php echo $hd->nama_kategori;?>
                <p><?php echo $hd->isi;?></p>
              </div>
            </div>
          <?} ?>
 				</div><!-- artikel -->
 				<div class="col-md-4"> <!-- sidebar -->
 					<p>Kategori</p>
 					<p>
          <ol>
            <?php foreach($kategori as $kat){ ?>
              <li>
              <a href="#"><?php echo $kat->nama_kategori; ?></a>
              </li>
            <?php }?>
          </ol>   
          </p>
 				</div> <!-- sidebar -->
 			</div>
 		</div> <!-- /isi dan sidebar -->

 		<div class="col-md-12"><!-- footer -->
 			Ini footer
 		</div><!-- footer -->

 	</div> <!-- row -->
 </div>
 <!-- /kontainer -->
 </body>
</html>

 

Recommended For You

About the Author: adyrhs

Leave a Reply

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