Skip to main content

pagination @ comment blog

July 29, 2009 by cah_ajus

cah_ajus's picture

hey para master.., saia mau tanya ttg pagination
saia mencoba membuat blog dgn mengikuti tutorial "create blog in 20 minute"yg ad d youtube, tapi saia mengalami kesulitan ketika ingin menambahkan pagination pada halaman comment blog.
tolong bantuannya...,
file controllernya spt ini :

class Comment extends Controller 
{
 
	function Comment() 
	{
		parent::Controller();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->database();
	}
 
	function index()
               {	
 
                }
 
	function comments()
	{
		$data['title']="My Comment Title";
		$data['heading']="My Comment Heading";
 
		$this->db->where('entry_id',$this->uri->segment(3));
		$data['hasil']=$this->db->get('comments');
		$this->load->view('comment_view',$data);
 
		//load pagination class
    	$this->load->library('pagination');
 
    	// Pengaturan konfigurasi untuk pagination
    	$config['base_url'] = base_url().'index.php/comment/comments/';
		$this->db->where('entry_id',$this->uri->segment(3));
		$this->db->from('comments');
    	$config['total_rows'] = $this->db->count_all_results();
    	$config['per_page'] = '4';
    	$config['first_link'] = 'Awal';
    	$config['last_link'] = 'Akhir';
    	$config['next_link'] = 'Selanjutnya';
    	$config['prev_link'] = 'Sebelumnya';
    	$this->pagination->initialize($config);
		//$id=$this->uri->segment(3);
    	//load model and ambil hasilnya
    	$this->load->model('Pagination_comment');
    	$data['hasil'] = $this->Pagination_comment->AmbilData( $config['per_page'],$this->uri->segment(3));
 
 
    	// load the view
    	$this->load->view('comment_view',$data);*/
	}
 
	function comment_insert()
	{
	$this->db->insert('comments',$_POST);
 
	redirect('comment/comments/'.$_POST['entry_id']);
	}
 
}
 
sebelumnya ada satu controller yg mengacu ke controller comment/comments dengan perintah :
<p><?=anchor('comment/comments/'.$row->id_berita,'Comments');

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

uri segment

July 30, 2009 by eien, 1 year 19 weeks ago
Comment: 5594

eien's picture

pagination CI bekerja dengan cara mengambil $config['uri_segment'](defaultnya segment yang ke-3) dan $config['per_page'], lalu menggunakan fungsi LIMIT untuk membatasi record yang dikembalikan.

jadi misal $config['per_page'] diisi 4 dan $config['uri_segment'] diisi 3 (default), maka

comment/comments akan mengambil record dari mulai record ke 0 (karena uri segment ke-3nya kosong) sebanyak 4 record

comment/comments/5 akan mengambil record dari mulai record ke 5 sebanyak 4 record

namun dalam kasus ini, uri segment yang ketiga udah anda gunakan buat menampung id_berita, jadi untuk paginationnya mungkin perlu menggunakan segment keempat

function comments()
    {
        $data['title']="My Comment Title";
        $data['heading']="My Comment Heading";
 
        $this->db->where('entry_id',$this->uri->segment(3));
        $data['hasil']=$this->db->get('comments');
        //$this->load->view('comment_view',$data); di bawah udah diload viewnya
 
        //load pagination class
        $this->load->library('pagination');
 
        entry_id=$this->uri->segment(3);
        // Pengaturan konfigurasi untuk pagination, entry_id tetap harus dibawa ke setiap halamannya
        $config['base_url'] = base_url().'index.php/comment/comments/'.$entry_id.'/'; 
 
        $this->db->where('entry_id',$entry_id);
        $this->db->from('comments');
        $config['total_rows'] = $this->db->count_all_results();
        $config['per_page'] = '4';
        $config['uri_segment'] = 4; //bagian ini perlu ditambahkan
        $config['first_link'] = 'Awal';
        $config['last_link'] = 'Akhir';
        $config['next_link'] = 'Selanjutnya';
        $config['prev_link'] = 'Sebelumnya';
        $this->pagination->initialize($config);
        //$id=$this->uri->segment(3);
        //load model and ambil hasilnya
        $this->load->model('Pagination_comment');
        $data['hasil'] = $this->Pagination_comment->AmbilData( $config['per_page'],$this->uri->segment(4));
        $data['page']=$this->pagination->create_links(); //berisi navigasi paginationnya
 
        // load the view
        $this->load->view('comment_view',$data);
    }

terima kasih

July 30, 2009 by cah_ajus, 1 year 19 weeks ago
Comment: 5595

cah_ajus's picture

mantappp......,!!
terima kasih atas jawabannya...,sangat membantu..,
^_^

kembali

July 30, 2009 by eien, 1 year 19 weeks ago
Comment: 5596

eien's picture

sama-sama ^ ^

Premium Drupal Themes by Adaptivethemes