The error:
function get_modal_awal(){
date_default_timezone_set('Asia/Jakarta');
$sql = "SELECT modal_awal
FROM trans_modal_awal
WHERE date_format(tanggal,'%Y-%m-%d') = '".date('Y-m-d')."'
AND kode_kasir = ".$_SESSION['kode_user']."";
return $this->db->query($sql)->row()->modal_awal; //the error goes here
}
my view code like this:
<input readonly class="form-control" type="text" id="modal_awal"name="modal_awal" value="<?php echo $modal_awal;?>"></input>
my controller:
$data['modal_awal'] = $this->Modal_kasir_model->get_modal_awal();
What should I do?
You are assuming that your SQL always returns a row. If it doesn't, then there is no object to read. I would try to trap this:
function get_modal_awal(){
date_default_timezone_set('Asia/Jakarta');
$sql = "SELECT modal_awal
FROM trans_modal_awal
WHERE date_format(tanggal,'%Y-%m-%d') = '".date('Y-m-d')."'
AND kode_kasir = ".$_SESSION['kode_user']."";
//Get row
$result = $this->db->query($sql);
if($result){
$row = $result->row();
if($row){
//return row if set
return $row->modal_awal;
}
}
//return nothing, and deal with it in the calling code
return false;
}
It's also not a bad idea to wrap your SQL calls in a try ... catch
block.
function get_modal_awal()
{
date_default_timezone_set('Asia/Jakarta');
$sql = "SELECT modal_awal
FROM trans_modal_awal
WHERE date_format(tanggal,'%Y-%m-%d') = '".date('Y-m-d')."'
AND kode_kasir = ".$_SESSION['kode_user']."";
$res = $this->db->query($sql);
return $res->row('modal_awal');
}
Use above code