Note that there are some explanatory texts on larger screens.

plurals
  1. POphp difference between 1 and '1'
    primarykey
    data
    text
    <p>I have struggle with this problem for hours. I have this method my model (codeigniter) :</p> <pre><code>public function get_umat($kelas1 = 0, $kelas2 = 0) { $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('msumat'); $this-&gt;db-&gt;join('mskelas', 'msumat.kelas_id = mskelas.kelas_id'); if($kelas1 != 0) { echo $kelas1; var_dump($kelas1); $this-&gt;db-&gt;where('mskelas.kelas_id', $kelas1); } else if($kelas2 !=0) { echo '2'; $this-&gt;db-&gt;where('mskelas.kelas_id', $kelas2); } else if($kelas1 != 0 &amp;&amp; $kelas2 !=0) { echo '3'; $this-&gt;db-&gt;where('mskelas.kelas_id BETWEEN $kelas1 AND $kelas2'); } return $this-&gt;db-&gt;get(); } </code></pre> <p><strong>EDIT :</strong> The one that <strong>not</strong> working is in this line of code (taken from above) :</p> <pre><code>$this-&gt;db-&gt;where('mskelas.kelas_id', $kelas1); </code></pre> <p>Its <strong>not</strong> working when i called this method in my controller, like this :</p> <pre><code>$this-&gt;backend_m-&gt;get_umat($_POST['ddl_kelas1'], $_POST['ddl_kelas1']); </code></pre> <p>I get '1'(<strong>String</strong>) when i vardump the <code>($_POST['ddl_kelas1']</code></p> <p>Then i try to <strong>change the parameter in the controller</strong>, but its still <strong>not</strong> working :</p> <pre><code> $this-&gt;backend_m-&gt;get_umat(1, $_POST['ddl_kelas1']); </code></pre> <p>Desperately, <strong>i tried to change the parameter directly in the model, and its working</strong> :</p> <pre><code>public function get_umat($kelas1 = 1, $kelas2 = 0) </code></pre> <p>Whats going on here?i think it has something to do with the difference of 1 (int) and '1' (String). Thanks :D</p> <p>Edit 2 : After some experiment, i think variable type is not the problem here. Thats because : I tried to do this in my <strong>controller</strong> :</p> <pre><code> if(isset($_POST['btnSearch'])) { $test = (int)$_POST['ddl_kelas1']; $this-&gt;backend_m-&gt;get_umat($_POST['ddl_kelas1'] , $_POST['ddl_kelas1']); } </code></pre> <p>And this in my model :</p> <pre><code>if((int)$kelas1 != 0) { echo $kelas1; var_dump($kelas1); $this-&gt;db-&gt;where('mskelas.kelas_id', $kelas1); } </code></pre> <p>What i get is, the <code>$kelas1</code> is already an int now, but the code is still <strong>not</strong> working. I think it has something to do with <code>$_POST</code> in my <code>select</code> in html, or something else :D</p> <p>However, this code is still working perfectly (just like above, i manually changed the parameter to 1 (integer)) :</p> <pre><code>public function get_umat($kelas1 = 1, $kelas2 = 0) </code></pre> <p>This is my full controller code :</p> <pre><code>public function index() { //table $data_umat = $this-&gt;backend_m-&gt;get_umat()-&gt;result(); $this-&gt;table-&gt;set_heading( 'No', 'Nama', 'Kelas', 'Alamat', 'Sekolah', 'Nomor Telepon', 'Keterangan' ); $table_template = array('table_open' =&gt; '&lt;table border="1" id="custom_table"&gt;'); $this-&gt;table-&gt;set_template($table_template); $no = 1; foreach($data_umat as $list_temp) { $this-&gt;table-&gt;add_row( $no++, $list_temp-&gt;nama, $list_temp-&gt;kelas, $list_temp-&gt;alamat, $list_temp-&gt;sekolah, $list_temp-&gt;no_tlpn, $list_temp-&gt;keterangan ); } $data_kelas = $this-&gt;backend_m-&gt;get_kelas()-&gt;result(); $data['list_kelas'][0] = 'Pilih Kelas'; foreach($data_kelas as $row) { $data['list_kelas'][$row-&gt;kelas_id] = $row-&gt;kelas; } $data['table'] = $this-&gt;table-&gt;generate(); $this-&gt;load-&gt;view('backend/home_v', $data); if(isset($_POST['btnSearch'])) { //$test = (int)$_POST['ddl_kelas1']; $test = (int)$this-&gt;input-&gt;post('ddl_kelas1'); $this-&gt;backend_m-&gt;get_umat($test , $_POST['ddl_kelas1'], $_POST['txtNama']); } } </code></pre> <p>Pretty confusing, meh?</p> <p><strong>EASY ANSWER FOR THOSE WHO HAVE THE SAME PROBLEM</strong> Use <code>$_POST</code> or <code>$this-&gt;input-&gt;post()</code> in model, <strong>not</strong> in controller. So we <strong>dont</strong> have to call the model's method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload