i have models like this
function getlastval($prefix,$namatable, $namafield)
{
$this->db->order_by($namafield,'DESC');
$key = $this->db->get($namatable);
if($key->num_rows()>0)
{
$lastval= $key->row()->Kode_Material_Jasa;
$lastval = explode($prefix,$lastval);
$lv = $lastval[1]; //line 18 refer to this line
//return ltrim($lv, '0');
}
else
{
return 0;
}
}
function gencode($prefix, $digit, $lastval)
{
return $prefix . str_pad($lastval+1, $digit, '0', STR_PAD_LEFT);
}
it works on the earlier controller
function addkategorimaterial()
{
$this->load->model('m_admin');
$lastval = $this->m_admin->getlastval('KKMJ','ms_kategori_material','Kode_Kategori_Material_Jasa');
$data['nextval'] = $this->m_admin->gencode('KKMJ',3,$lastval);
$data['title'] = 'QB Tambah Kategori Material';
$this->load->view('head',$data);
$this->load->view('content/add_kategori_material');
}
it returns KKMJ001 but then its not working on the other controller
$this->load->model('m_admin');
$lastval = $this->m_admin->getlastval('KMJ','ms_material_jasa','Kode_Material_Jasa');
$data['nextval'] = $this->m_admin->gencode('KMJ',3,$lastval);
$data['kkmj']='';
$data['title'] = 'QB Tambah Material / Jasa';
print_r($data['nextval']);
print_r($lastval);
so the function would return KMJ001 and the error goes like this

how do i resolve this ?
I don’t think that
explode()is the function you’re after. Theexplode()function splits a string into an array by a delimiter.It doesn’t take a ‘prefix’ as it’s first parameter as you’ve chosen (semantically). It takes the delimiter as it’s first parameter.
Since you’re passing
KMMJas the delimiter, the only way that you’re going to get an array index of 1 is if the string returned from$key->row()->Kode_Material_Jasa;is something like:fooKMMJbar, which would produce:array('foo', 'bar')…which is highly improbable.Anyway, you’ve called
getlastval('KMJ'...)andgetlastval('KMMJ'...), you sure they shouldn’t be the same?