Senin, 03 Juni 2013

Membuat Halaman Paging Untuk Menampilkan Data pada CodeIgniter Menggunakan Library Pagination

Membuat Halaman Paging untuk Menampilkan Data pada CodeIgniterMembuat Halaman Paging Untuk Menampilkan Data pada CodeIgniter Menggunakan Library Pagination dibutuhkan karena bisa jadi data yang ditampilkan sangat banyak, katakanlah seribu atau beberapa puluh ribu baris sehingga tidak akan bisa ditampilkan dalam satu halaman. Artikel ini juga merupakan sambungan dari artikel CodeIgniter yang lain yaitu Menampilkan Data(View), Mengedit Data (Update), dan Menghapus (Delete).

Buatlah view dengan nama /webci/application/views/vguru.php seperti ini :

<?php
echo anchor('guru/tambah', 'Tambah Data');
echo $isi;

echo anchor('guru/tambah', 'Tambah Data');
echo "<br/>";
echo $linkhalaman;
echo $isihalaman;
?>

Kemudian kita buat Model bernama webci/application/models/mguru.php seperti ini :

<?php
 class Mguru extends CI_Model
 { 
  function jmlbaris()
  {
   return $this->db->count_all('guru');
  }
  function getdatahalaman($limit=3,$offset=0)
  {
  $this->db->select('*');
  $this->db->from('guru');
  //$this->db->join('departemen','karyawan.kddepartemen = departemen.kddepartemen','left');
  $this->db->order_by('nip','asc');
  $this->db->limit($limit,$offset);
  $ambil = $this->db->get();
  $this->table->set_empty('&nbsp');
  $this->table->set_heading('No','NIP','Nama','Alamat','Tgl Lahir','Tunjangan','Action'); //,'Agama','Departemen','Action');
  $i=$offset;
  if($ambil->num_rows() > 0)
   {
   foreach($ambil->result() as $baris)
    {
     $i++;
     $this->table->add_row($i,$baris->nip,$baris->nama,$baris->alamat,$baris->tgllahir,$baris->tunjangan, //,$baris->agama,$baris->nmdepartemen,
     anchor('guru/ubah/'.$baris->nip,'Edit',array('class'=>'linkmerah')).' '.anchor('guru/hapus/'.$baris->nip,'Hapus',
     array("onclick"=>"return confirm('Anda Yakin?');")));
    }
    return $this->table->generate();
   }
  
  }
  function getdata()
  {
  //$this->load->library('table');
  //$query = $this->db->query("select * from guru");
  //return $this->table->generate($query);
  //--------------------
  //$ambil = $this->db->get('guru');
  //--------------------
  $this->db->select('*');
  $this->db->from('guru');
  //$this->db->join('departemen','karyawan.kddepartemen = departemen.kddepartemen','left');
  $ambil = $this->db->get();
  $this->table->set_empty('&nbsp');
  $this->table->set_heading('No','NIP','Nama','Alamat','Tgl Lahir','Tunjangan','Foto','Action');
  $i=0;
  if($ambil->num_rows() > 0)
   {
   foreach($ambil->result() as $baris)
    {
     $config_img = array('src'=>'images/'.$baris->foto,'width'=>'100');
     $i++;
     $this->table->add_row($i,$baris->nip,$baris->nama,$baris->alamat,$baris->tgllahir,$baris->tunjangan,img($config_img),anchor('guru/ubah/'.$baris->nip,'Edit',array('class'=>'linkmerah')).' '.anchor('guru/hapus/'.$baris->nip,'Hapus',
     array("onClick"=>"return confirm('Anda Yakin?');")));
    }
    return $this->table->generate();
   }
  
  }
 }
?>

Langkah terakhir buatlah kode program ini pada Controller guru.php yang ada di webci/application/controllers/guru.php :

<?php
class Guru extends CI_Controller{
 function index($offset=0){
  $this->load->model('mguru');
  $data['isi'] = $this->mguru->getdata();
  //$this->load->view('vguru',$data);
 
  $per_halaman = 3;
  $this->load->model('mguru');
  $this->load->library('pagination');
  $config['base_url'] = site_url('guru/halaman');
  $config['total_rows'] = $this->mguru->jmlbaris();
  $config['per_page'] = $per_halaman;
  $config['uri_segment'] = $per_halaman;
  $this->pagination->initialize($config);
  $data['linkhalaman'] = $this->pagination->create_links();
  $data['isihalaman'] = $this->mguru->getdatahalaman($per_halaman,$offset);
  //$this->load->view('vguruhalaman',$data);
  
  $this->load->view('vguru',$data);
 }
 function halaman($offset=0){
  $per_halaman = 3;
  $this->load->model('mguru');
  $this->load->library('pagination');
  $config['base_url'] = site_url('guru/halaman');
  $config['total_rows'] = $this->mguru->jmlbaris();
  $config['per_page'] = $per_halaman;
  $config['uri_segment'] = $per_halaman;
  $this->pagination->initialize($config);
  $data['linkhalaman'] = $this->pagination->create_links();
  $data['isi'] = $this->mguru->getdatahalaman($per_halaman,$offset);
  $this->load->view('vguruhalaman',$data);
 }
 function tambah()
 {
  if($this->input->post('submit')){
   $this->load->model('mguru');
   $this->mguru->simpandata();
   //$this->do_upload();
   redirect('guru/index');
  }
  //$this->load->model('mguru');
  //$data['barisdepartemen'] = $this->mguru->getdatadepartemen();
  $this->load->view('vaddguru'); //,$data);
 }
 function hapus($id)
 {
  //$this->db->delete('guru',array('nip'=>$id));
  $this->db->where('nip',$id);
  $this->db->delete('guru');
  redirect('guru/index');
 }
 function ubah($id)
 {
  if($this->input->post('submit'))
  {
   $this->load->model('mguru');
   $this->mguru->ubahdata($id);
   //$this->do_upload();
   redirect('guru/index');
  }
  else
  {
   $this->load->model('mguru');
   $data['baris'] = $this->mguru->selectdata($id);
   //$data['barisdepartemen'] = $this->mguru->getdatadepartemen();
   $this->load->view('veditguru',$data);
  }
 }
 function do_upload()
 {
  $config['upload_path'] = './images/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100000';
  $config['max_width']  = '10240';
  $config['max_height']  = '7680';

  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());

   $this->load->view('upload_form', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   //print_r($this->upload->data());
   $datafoto=$this->upload->data();
   $nm_file = time().$datafoto['orig_name'];
   $this->load->model('mguru');
   $this->mguru->update_foto($nm_file);
   copy('images/'.$datafoto['orig_name'], 'images/'.$nm_file);
   //echo $datafoto['orig_name'];
   //$this->load->view('upload_success', $data);
  }
 }
}
?>

Hasilnya, panggil halaman Gurudengan url http://127.0.0.1/webci/guru/, maka akan muncul tabel data dengan paging :

Membuat Halaman Paging untuk Menampilkan Data pada CodeIgniter

Project selengkapnya dapat didownload disini. Jika kesulitan download, ini caranya

Semoga bermanfaat.

Tidak ada komentar:

Posting Komentar