首页 > 解决方案 > EDIT: how to automatically add data from a field when called

问题描述

how to automatically add data from a field when called. example: value of field is = 1AA/example/example

iam using foreach

foreach ($datanomor as $row){
$var=$row->MaxCode;
echo $var;
}

the result is

1AA/example/example

i want to add operation on (1+ 1AA/example/example)

so what must i do when i call the field, result will show:

2AA/example/example

***edit have table with name t_surat_keluar

and query to call value of field in my controller is :

$datanomor = $this->db->query("SELECT max(nomor_surat) as maxKode")
    ->('FROM t_surat_keluar where id_jenissurat=$row->id_jenissurat')
    ->result();
$data['datanomor']=$datanomor; 

the value is 001/smk/ck/2018

In my view i run this script to call that value:

foreach ($datanomor1 as $row) {
    $var = $row->maxKode;
    echo $var;
}

Now I want to ask, how can I change directly that value (001/smk/ck/2018) to (002/smk/ck/2018) when i calling from script ?.

sorry my english is bad.

***edit now i can change directly that value with this script

foreach ($datanomor1 as $row){
$var=$row->maxKode;
$var[2] = (int)$var[2]+1;
 echo $var;
            }

the new problem is when last value (009/smk/ck/2018) if i use this script:

$var[2] = (int)$var[2]+1;

output will show (**001**/smk/ck/2018) not (**0010**/smk/ck/2018)

标签: phpmysql

解决方案


据我了解,你想对你的 var 上的第一个字符进行操作,所以你可以从 var 中选择它作为字符

foreach ($datanomor as $row){
$var=$row->MaxCode;
//select the first char and casting it to integer then increment it 
$var[0] = (int)$var[0]++;

echo $var;
}

推荐阅读