~:Catatan Belajar MazHel:~

CodeIgniter : paging an array

Posted in PHP, Programming by m42h31 on 26 Agustus 2009

Anda capek nyari di google gimana caranya paging array di codeigniter tapi gak ketemu-ketemu. tah, berarti anda senasib dengan saya..hue..hue..hue… alhamdulillah setelah menegak segelas air putih tanda berbuka puasa baru kefikiran caranya (lagi lemot euy). alhamdulillah mungkin inilah salah satu berkahnya puasa..

contoh saya punya array :

$dataku = array("bapak","ibu","kakak","saya","adik");

maka klu di print_r($dataku) muncul  hasil :

Array([0]=>bapak [1]=>ibu [2]=>kakak [3]=>saya [4]=>adik)

baiklah saya akan coba penampilkan perpage dengan perindex array diatas, caranya begini :

code ini ditaruh di Controller :

$config['base_url'] = site_url('test/test');
$config['total_rows'] = count($dataku);
$config['per_page'] = '1';
$this->pagination->initialize($config);

$no = 0; $next = 0; $redudanceno = 0; $noredudanceno = array() ;
$simpan = array();
foreach($dataku as $key=>$val){
if($key<$config[‘per_page’] && $key >=0 ){
if(!$this->uri->segment(3)){
$no++;
}else {
$next++;
if($next>0){
$no = $next+$config[‘per_page’]*($this->uri->segment(3)/$config[‘per_page’]);
}else{
$no = $this->uri->segment(3);
}
}
if($redudanceno!=$no && $redudanno < $config[‘total_rows’]){
$noredudanceno[] = $no;
}
$redudanceno = $no;
}//endif

}//end foreach

$count = count($noredudanno);

for($k=$noredudanceno[0];$k<$noredudanceno[$count-1];$k++){
$simpan[] = $dataku[$k];
}
unset($dataku,$noredudanno,$redudanno);
$data[‘dataku’]= $simpan;
$data[‘paging’] = $this->pagination->create_links();
$this->load->view(“view_data”,$data);

Kemudian di view codenya seperti ini :

<?=$paging; ?>

<?

foreach($dataku as $rows){ ?>

<td><?=$rows?></td>

<? } ?>

Done..!! selamat mencoba..

referensi : samsonasik

2 Tanggapan

Subscribe to comments with RSS.

  1. nina said, on 1 September 2010 at 04:59

    controller :
    ===================
    class Test extends Controller {
    function Test(){
    parent::Controller();
    }

    function index(){
    $this->load->library(‘pagination’);
    $dataku =array();
    $data_2 =array();
    $dataku = array(“bapak”,”ibu”,”kakak”,”saya”,”adik”);
    $data_2 = array(“ponakan”,”nenek”,”kakek”,”paman”,”tante”);
    $arr_result = array_merge($dataku,$data_2);
    $config[‘base_url’] = site_url(‘test/index’);
    $config[‘total_rows’] = count($arr_result);
    $config[‘uri_segment’] = 3;
    $config[‘per_page’] = ‘2’;
    $this->pagination->initialize($config);
    $page = $this->uri->segment(3);
    $arr_display = array_slice($arr_result,$page,$config[‘per_page’]);
    $data[‘dataku’]= $arr_display;
    //print_r( $resArray);exit;
    $data[‘paging’] = $this->pagination->create_links();
    $this->load->view(‘test’,$data);
    }

    }

    view :
    =========

    Data :

  2. sancoLgates said, on 20 Januari 2014 at 10:14

    masih kurang lengkap bro kurang paham ane
    tolong cek web ane gan di bagian kanan “ALSO VISIT THIS WEBSITE” gak bisa sejajar thanks before .


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: