Reputation: 400
In my database field i have varchar field like this format HPDO1209180000.What i am doing is if i dont have any data i am adding four digits at last it is working fine.And if if it is there i am incrementing one each time.but the problem is after 0001 also incrementing all the 0001 only not 0002 like that.I am doing like this please help me.thanks in Advance.
$this->db->select('grmno');
$this->db->from('procurement_receive_material');
$this->db->where('DATE(createdon)',$currentdate);
$this->db->where('SUBSTRING(grmno,1,10)',$grn_code);
$this->db->order_by('prmid','desc');
$query = $this->db->get();
if($query->num_rows()>0){
$output = $query->row();
$grmNumber = $output->grmno;
//after 0001 also it incrementing 0001 not incrementing;
$inrno=str_pad((int)$grmNumber+1, 4, 0, STR_PAD_LEFT);
$grmNumber=$vendor_result.$branch_result.$currentDate.$currentmonth.$currentyear.$inrno;
}else{
$grmNumber = $vendor_result.$branch_result.$currentDate.$currentmonth.$currentyear.'0000';
}
Upvotes: 0
Views: 911
Reputation: 548
If I'm reading this correctly, it looks like this line:
$inrno=str_pad((int)$grmNumber+1, 4, 0, STR_PAD_LEFT);
Is taking the int value of $output->grmno. And this field, again if I'm interpreting your problem directly, can be evaluated as something like this:
HPDO1209180001
The integer value of such a string would be 0. Hence, why they always end up as 0001.
To fix this, you need to grab the last 4 characters of the string, and increment only that. For example:
$suffix = substr($grmNumber, -4);
$newsuffix = intval($suffix) + 1;
$irno = str_pad($newsuffix, 4, 0, STR_PAD_LEFT);
This would properly grab the last 4 digits, add one to the value, and again pad it with zeros to be added to the new string.
Upvotes: 1