2

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.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • 1
    @BlazeTama I was right with the answer btw. http://stackoverflow.com/questions/5938221/string-compare-on-a-bool Just to be convinced I asked another question http://stackoverflow.com/questions/15462279/php-if-condition-with-boolean and got downvoted, didn't realize question was asked previously. :( – ro ko Mar 17 '13 at 15:27

2 Answers2

4

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);
itsme
  • 48,972
  • 96
  • 224
  • 345
  • nope I was in the process of adding the comment. Actually I like your hair style (y). You're doing exactly the same what he's doing adding the braces wouldn't help much. Checking for !$result or FALSE both are same – ro ko Mar 17 '13 at 14:21
  • Its not me, and i upvoted it :D Yes, i know something like this will work(playing with if position, i just need to switch the 2nd with 3rd), but what i want is the explanation of whats going on. Thanks :D – Blaze Tama Mar 17 '13 at 14:21
  • @ro ko check better my answer i corrected enteire if sintaxt too – itsme Mar 17 '13 at 14:23
  • 1
    @BlazeTama i need your method ansensi() pls – itsme Mar 17 '13 at 14:23
  • @BlazeTama It was me, he was referring to me :) Not sure how that works and your doesn't though. – ro ko Mar 17 '13 at 14:23
  • @donotusetabtodigitthisnick was the second statement if from the beginning or you edited it, if you didn't edit it my mistake; sorry for it still not very convincing answer. Let's fix it (y) I agree on that. – ro ko Mar 17 '13 at 14:25
  • @BlazeTama check i updated answer, you can't return a db method, use some value in that *return*, also check new if statements, all will works fine now ;) – itsme Mar 17 '13 at 14:30
  • @donotusetabtodigitthisnick Thank you very much for your help :D Yes, your answer is true, but what i really want is the explanation on whats going on in my code :D – Blaze Tama Mar 17 '13 at 14:41
  • @BlazeTama i told you what's going on, you can't return a db method and you should use a correct if statements – itsme Mar 17 '13 at 14:43
  • I accepted your answer because you tell me to use the var_dump(), the answer is : echo will convert true to 1 :D Thanks for your effort :D And we can return a method (at least in CI) – Blaze Tama Mar 17 '13 at 14:46
  • @BlazeTama i think all CI methods returns TRUE or FALSE, so take care on this ;) , so, in CI, to check if a method returns true use **if(method()){}** , to check if method is returning false use **if(!method()){}** – itsme Mar 17 '13 at 14:47
  • @donotusetabtodigitthisnick okay, thanks for your additional help :D – Blaze Tama Mar 17 '13 at 14:58
2

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.

ro ko
  • 2,906
  • 3
  • 37
  • 58
  • Do you think its because i get the value (1) NOT from 'return 1;' but from codeigniter (from '$this->idb->insert()')? Yes i'm very sure, i can provide a screenshoot and full code if you want :D – Blaze Tama Mar 17 '13 at 14:20
  • @ro ko this is not a fix $result === 'sudah absen' it's exactly he same to $result == 'sudah absen' , be serious ;) – itsme Mar 17 '13 at 14:33
  • @donotusetabtodigitthisnick seriously?? http://php.net/manual/en/language.operators.comparison.php – ro ko Mar 17 '13 at 14:35
  • Thanks :D, but why when i use echo $result;, the value that appear is 1, not TRUE? – Blaze Tama Mar 17 '13 at 14:35
  • @BlazeTama it depends on what your absensi() method is returning – itsme Mar 17 '13 at 14:37
  • @BlazeTama yes for echo $result or print will output 1, well that's how php works (or may be I don't know the reason). try var_dump to see the type. – ro ko Mar 17 '13 at 14:38
  • @donotusetabtodigitthisnick i think the method is returning 1, because when i echo it, the value is 1, not TRUE :D – Blaze Tama Mar 17 '13 at 14:38
  • === means identical i.e. type should be same as well. – ro ko Mar 17 '13 at 14:39
  • @BlazeTama the right way to check what somenthing returning is using *var_dump();* not *echo* echo is used only for strings, maybe your method is returning TRUE and echo TRUE ; prints 1 – itsme Mar 17 '13 at 14:44
  • @ro ko rly for me you can remove === part from your answer, rly not usefull – itsme Mar 17 '13 at 14:46
  • Thank you very much for your effort, its too bad i can only give 1 upvote and accept 1 answer (which is suited the best for other reader/visitor) :( – Blaze Tama Mar 17 '13 at 14:47
  • @ro ko i think you can remove your -1 i'll remove mine ;) – itsme Mar 17 '13 at 14:53
  • @donotusetabtodigitthisnick I'll leave it be, just going with documentation. Cool with me for remoing -1 part. – ro ko Mar 17 '13 at 14:54
  • @BlazeTama Oo uhmm you newbie :D and question is newbie, but uhm ok i'll upvote you you honest dude – itsme Mar 17 '13 at 15:00