Welcome Guest, Not a member yet? Register   Sign In
Change integer value if first digit is zero(0)
#1

[eluser]phparif[/eluser]
Please any one help me about :
If i start a integer number with 0(zero), its value is change when execute, suppose if i execute this active record
$rol = 074052;
$this->db->set('AdviserId', 'A00002')
->where('DepartmentId', 'C06')
->where('StudentId',$rol)
->update('student_info');
it generate a query like this
UPDATE `student_info` SET `AdviserId` = 'A00002' WHERE `DepartmentId` = 'C06' AND `StudentId` = 30762;
in my active record i put StudentId = 074052, but in generated query why `StudentId` = 30762
#2

[eluser]louisl[/eluser]
Make the var a string as 0xx as integer will drop the 0 as it's a number
#3

[eluser]phparif[/eluser]
@louisl, I use this $arr = (string)074052;
but result is same previous[`StudentId` = 30762]
#4

[eluser]louisl[/eluser]
$rol = '07405l2';
Try that, I'm assuming your db student Id field is varchar
#5

[eluser]phparif[/eluser]
Actually Roll no is posted by user which is in textBox(txtextra) separated by coma(Example: 074051,074052,074053). when submit it call a function like this

function SelectAdviser(){
$data = explode(",",$this->input->post('txtextra'));
$this->db->set('AdviserId', 'A00002')
->where('DepartmentId', 'C06')
->where_in('StudentId',$data)
->update('student_info');
}

Now generated query all is ok, but roll is generate unexpected number, as a result of prefix 0(zero).

I try below two way but result is same :

#1)
function SelectAdviser(){
$data = explode(",",$this->input->post('txtextra'));
$data = array_map('strval',$data);
$this->db->set('AdviserId', 'A00002')
->where('DepartmentId', 'C06')
->where_in('StudentId',$data)
->update('student_info');
}
#2)
function SelectAdviser(){
$data = explode(",",$this->input->post('txtextra'));
foreach ($data as $index => $val) $data[$index] = (string) $val;
$this->db->set('AdviserId', 'A00002')
->where('DepartmentId', 'C06')
->where_in('StudentId',$data)
->update('student_info');
}
#6

[eluser]aquary[/eluser]
I run a little code to test, and it seems to be a part in active record escaped/protected the 0xxxxx pattern.

EDIT: errr.... no, it's PHP engine convert those number. Maybe PHP treats the 0xxxx pattern as a hex or something similar back to integer.

EDIT: It's OCTAL number
www.php.net/manual/en/language.types.integer.php

BUT, anyway, since the data came from input/textarea with comma as a separator, this code works fine:

Code:
$val='074051,074052,074053';
   $val=explode(',', $val);
   foreach($val as $v){
     $this->db->where('id',$v)
        ->get('announcements');

      echo $this->db->last_query();
      echo '<br><br>';
}
//Produced these:
//SELECT * FROM (`announcements`) WHERE `id` = '074051'

//SELECT * FROM (`announcements`) WHERE `id` = '074052'

//SELECT * FROM (`announcements`) WHERE `id` = '074053'


#7

[eluser]phparif[/eluser]
Thanks.....
#8

[eluser]louisl[/eluser]
Hmm that is weird because if txtextra is a textbox with comma seperated values then it's a string to start with. So you were essentially doing #3 so it does look like active record is doing a conversion.

eg:-
Code:
1).
$arr = array(0123,0345,0567,11);
echo '<pre>';
var_dump($arr);
echo '</pre>';

2).
$arr = array('0123','0345','0567',11);
echo '<pre>';
var_dump($arr);
echo '</pre>';

3).
$arr = explode(',', '0123,0345,0567,11');
echo '<pre>';
var_dump($arr);
echo '</pre>';


output:-
array(4) {
  [0]=>
  int(83)
  [1]=>
  int(229)
  [2]=>
  int(375)
  [3]=>
  int(11)
}
array(4) {
  [0]=>
  string(4) "0123"
  [1]=>
  string(4) "0345"
  [2]=>
  string(4) "0567"
  [3]=>
  int(11)
}
array(4) {
  [0]=>
  string(4) "0123"
  [1]=>
  string(4) "0345"
  [2]=>
  string(4) "0567"
  [3]=>
  string(2) "11"
}
#9

[eluser]phparif[/eluser]
Again thanks of all.




Theme © iAndrew 2016 - Forum software by © MyBB