Membuat Portal Berita Dengan CodeIgniter dan Bootstrap Lanjutan

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

Download Project

Tambahkan Code ini di dalam file Controller Home.

‘pagination’ => $this->pagination->create_links(),

‘total_rows’ => $config[‘total_rows’],

‘q’ => $q,

Perhatikan function dibawah ini

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(),
        'pagination' => $this->pagination->create_links(),
        'total_rows' => $config['total_rows'],
        'q' => $q,       	
    );
    $this->load->view('home/home',$data);

}

Kemudian pada View dalam folder home (home.php)

tambahkan :

<form class="navbar-form navbar-left" action="<?php echo site_url('home/index'); ?>" class="form-inline" method="get">
      <div class="input-group">
          <input type="text" class="form-control" name="q" value="<?php echo $q; ?>">
          <span class="input-group-btn">
              <?php 
                  if ($q <> '')
                  {
                      ?>
                      <a href="<?php echo site_url('home'); ?>" class="btn btn-default">Reset</a>
                      <?php
                  }
              ?>
            <button class="btn btn-primary" type="submit">Cari</button>
          </span>
      </div>
  </form>

untuk menggantikan form pencarian sebelumnya

 

tambahkan :

<div class="row">
    <div class="col-md-6">
        <a href="#" class="btn btn-primary">Total Record : <?php echo $total_rows ?></a>
</div>
    <div class="col-md-6 text-right">
        <?php echo $pagination ?>
    </div>
</div>

sehingga hasilnya kurang lebih seperti gambar dibawah ini ..

				<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 class="row">
           <div class="col-md-6">
               <a href="#" class="btn btn-primary">Total Record : <?php echo $total_rows ?></a>
       </div>
           <div class="col-md-6 text-right">
               <?php echo $pagination ?>
           </div>
       </div>

				</div><!-- artikel -->

Selanjutnya lakukan update pada code <!– sidebar –> seperti dibawah ini

	<div class="col-md-4"> <!-- sidebar -->
	<p>Kategori</p>
	<p>
         <ol>
           <?php foreach($kategori as $kat){ ?>
             <li>
             <a href="<?php echo base_url('home/kategori/'.$kat->id_kategori);?>"><?php echo $kat->nama_kategori; ?></a>
             </li>
           <?php }?>
         </ol>   
         </p>
				</div> <!-- sidebar -->

 

Kemudian tambahkan code di dalam Models (Home_model.php)

function total_rows_kategori($q = NULL,$id) {
    $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->group_start();
    $this->db->like('judul', $q);
$this->db->or_like('isi', $q);
$this->db->or_like('nama_kategori', $q);
$this->db->group_end();
$this->db->where('a.id_kategori',$id);
    return $this->db->count_all_results();
}

function get_limit_data_kategori($limit, $start = 0, $q = NULL,$id) {
    $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->group_start();
    $this->db->like('judul', $q);
$this->db->or_like('isi', $q);
$this->db->or_like('nama_kategori', $q);
$this->db->group_end();
$this->db->where('a.id_kategori',$id);
$this->db->limit($limit, $start);
      return $this->db->get()->result();
}

 

Selanjutnya tambahkan function baru di Controller Home.php

function kategori($id){

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

}

 

Hasil Akhir Sementara

Recommended For You

About the Author: adyrhs

Leave a Reply

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