epm.007
epm.007

Reputation: 7

how to send parameters array data from models to custom library and access the array in codeigniter..?

I have some problem for accessing array data in my custom library.

my model

    <?php if( ! defined('BASEPATH'))exit('No direct script access allowed');

      class Laporan_Gaji_M extends CI_Model {
          function __construct() {
          parent::__construct();
            $this->load->library("Payroll");
            $this->load->library("Payroll_J");
            $this->load->library("Payroll_G");
      }

      function data_ku() 
      {
       $sql = "SELECT a.idx, a.kode, a.jenis, a.nama, a.tgl_masuk, 
       a.status_karyawan, a.kelamin, a.status_tk, a.tunj_tetap, 
       a.tunj_jabatan, a.tunj_keahlian, a.medical_housing, a.upah_lembur, 
       a.gaji, c.nama AS nama_jabatan, a.kd_finger, b.* FROM 
       hrd_master_pegawai AS a LEFT JOIN hrd_laporan_absensi AS b ON 
       a.kd_finger = b.kode_finger AND b.bulan = '$bulan' LEFT JOIN 
       hrd_master_jabatan AS c ON a.jabatan = c.id_jabatan";

       $row = $this->db->query($sql);
    
          foreach ($row->result_array() as $value) {

            //print_r($value["jenis"]);

            $data = new Payroll_G($value); // i try send to my custom library and access the arrays data but not working.
    
          }
     } 
}

I try to get the $value["jenis"] in models and working fine, but when I try to get $value["jenis"] in my custom library. I got message

Severity: Notice Message: Undefined index: jenis Filename: libraries/Payroll_G.php**

here my custom library

<?php

class Payroll_G extends Payroll {

    function __construct($value = array()) {
        parent::__construct();
        $this->hitung($value);
    }

    function hitung($value) {
    print_r($value["jenis"]);
    // message => Undefined index: jenis
    
    }
}

Upvotes: 0

Views: 70

Answers (1)

chad
chad

Reputation: 838

The error occurs when you initially load the library inside the Models's construct since $value is empty. And you also initialize the class inside a loop repeatedly.

I suggest you use codeigniter's function calls convention:

Model:

foreach ($row->result_array() as $value) {

        $data = $this->Payroll_G->hitung($value);

      }

Library:

class Payroll_G extends Payroll {

function __construct() {
    parent::__construct();
}

function hitung($value=array()) {
print_r($value["jenis"]);
}
}

Upvotes: 0

Related Questions