简体   繁体   中英

model return value in codeigniter

I store the return value from my model in a $result :

$result = $this->absensi_m->absensi();

Based on what i return in the absensi() function, the value of $result will be one of these :

  • FALSE
  • 'sudah absen' (a String)
  • 1 (indicating success)

Then i try to see the $result value using echo $result; and it returns 1

But, i get a strange error when i try to use $result for some validations :

        if($result == FALSE)
            $data['result'] = 'Anda Belum Terdaftar, Silahkan Daftar Dahulu :D';
        else if($result == 'sudah absen')
            $data['result'] = 'Anda Sudah Absen Hari Ini :D';
        else 
            $data['result'] = 'Selamat Datang, '.$this->input->post('txtNama').'! ^^';

Although the value of $result is 1 , the second if ( $result == 'sudah absen' ) always return TRUE .

Whats going on here?Thanks :D

This is the absensi() function in my model :

public function absensi() {
        $nama = $this->input->post('txtNama');
        $tanggal = date('Y-m-d', now());

        $this->db->select('umat_id');
        $terdaftar = $this->db->get_where('msumat', array('nama' => $nama));

        $row = $terdaftar->row_array();
        $sudah_absen = $this->db->get_where('msabsensi', array('umat_id' => $row['umat_id'], 'absensi_tanggal' => $tanggal));

        if($terdaftar->num_rows() != 0 && $sudah_absen->num_rows() == 0)
        {
            $data_umat = array(
            'umat_id' => $row['umat_id'],
            'status' => 'H',
            'absensi_tanggal' => date('Y-m-d')
            );

            return $this->db->insert('msabsensi', $data_umat);
        }
        else if($sudah_absen->num_rows() != 0)
            return 'sudah absen';
        else
            return FALSE;
    }

ANSWER : I will answer here because you may be confused with a lot of comments/conversation.

I realized whats going on by using var_dump() and echo ( Thanks to both answers below ):

$result = $this->absensi_m->absensi();
var_dump($result);
echo $result;

The var_dump() returns bool(true) while the echo returns 1. This means that this problem is occured because i think the $result value is 1(integer), while the real value is TRUE(bool).

The conclusion is : echoing a variable with a TRUE(bool) value will result 1(integer), so use the var_dump() instead of echo .

first you can't return:

return $this->db->insert('msabsensi', $data_umat);

do this:

 $this->db->insert('msabsensi', $data_umat);
 return ...-> 1 or true or what you need

then you should do:

if(!$result){
            $data['result'] = 'Anda Belum Terdaftar, Silahkan Daftar Dahulu :D';
        }
    if($result == 'sudah absen'){
            $data['result'] = 'Anda Sudah Absen Hari Ini :D';
         }
       if($result && $result !== 'sudah absen'){
            $data['result'] = 'Selamat Datang, '.$this->input->post('txtNama').'! ^^';
          }

then use var_dump(); to check what result is:

var_dump($result);

Are you sure this is happening? are you sure $result=1 ?

I find it hard to believe. Here's a fiddle output to your code

your function returns true not 1

you should add else if($result === 'sudah absen') to strictly check for the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM