Membuat Portal Berita Dengan CodeIgniter dan Bootstrap lanjutan 3

Download file Terakhir disini

Hasil Akhir Sementara

Selanjutnya tambahkan function di dalam Home_model

function read($id){
	$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->where('a.id',$id);
    return $this->db->get()->row();
}

Kemudian ubah view (home.php)

yang tadinya seperti ini

<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>

Menjadi

<ul class="nav navbar-nav">
  <li><a href="<?php echo base_url('home/index');?>"><i class="glyphicon glyphicon-home"></i> HOME</a></li>
  <li><a href="<?php echo base_url('home/index');?>"> ARTICLE</a></li>
  <li><a href="<?php echo base_url('home/profile');?>"> PROFILE</a></li>
</ul>

Pada baris ke 70 yang sebelumnya

<?php echo $hd->judul;?> - <?php echo $hd->nama_kategori;?>
<p><?php echo $hd->isi;?></p>

ubah menjadi

<?php echo $hd->judul;?> - <?php echo $hd->nama_kategori;?>
<p><?php echo substr($hd->isi,0,75);?></p>
<p class="text-right"><a href="<?php echo base_url('home/read/'.$hd->id);?>" class="btn btn-success">Readmore . . .</a></p>

Selanjutnya update kembali view (home.php)

				<div class="col-md-8"> <!-- artikel -->
       <?php if($this->uri->segment('2') == '' OR $this->uri->segment('2') == 'index' OR $this->uri->segment('2') == 'kategori'){ ?> <!-- kondisi -->
         <?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 substr($hd->isi,0,75);?></p>
               <p class="text-right"><a href="<?php echo base_url('home/read/'.$hd->id);?>" class="btn btn-success">Readmore . . .</a></p>
             </div>
           </div>
           <hr/>
         <?php } ?>
         
       <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>
       <?php } else if($this->uri->segment('2') == 'read'){ ?> <!-- kondisi 2 -->
       <h1><?php echo $judul;?></h1>
       <p>Kategori : <?php echo $nama_kategori;?> <i class="glyphicon glyphicon-time"></i> <?php echo date('d/m/Y', strtotime($tgl_terbit));?></p>
       <center><img src="<?php echo $gbr;?>" class="img img-thumbnail"></center>
       <p style="margin-top:10px;margin-bottom: 10px;"><?php echo $isi;?></p>
       <?php } ?>
				</div><!-- artikel -->

 

tambahkan function baru di Controller (Home.php)

function read($id){
    $q = urldecode($this->input->get('q', TRUE));
    $row = $this->Home_model->read($id);
    if ($row) {
        $data = array(
    'id' => $row->id,
    'judul' => $row->judul,
    'isi' => $row->isi,
    'nama_kategori' => $row->nama_kategori,
    'tgl_terbit' => $row->tgl_terbit,
    'q'     => $q,
    'kategori'  => $this->Home_model->semua_kategori(),
    );            
    if(file_exists('gambar/'.$row->gbr))
    {
        if(!empty($row->foto)){
           $data['gbr'] = site_url('gambar/'.$row->gbr);
        }else{
           $data['gbr'] = site_url('gambar/no-image.jpg'); 
        }
    }else{
        $data['gbr'] = site_url('gambar/no-image.jpg');
    }
        $this->load->view('home/home', $data);
    } else {
        $this->session->set_flashdata('message', 'Record Not Found');
        redirect(site_url('home'));
    }

}

Hasil Akhir Sampai dengan Read Artikel

Download

Recommended For You

About the Author: adyrhs

Leave a Reply

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