dourunlao1642 2014-06-22 11:25
浏览 43
已采纳

在字符串php上添加序列号

I have these codes:

    //ambil nomor surat maksimum pada tabel surat_keterangan
$query = "SELECT MAX(nomor_surat) as noMax FROM surat_keterangan";
$result = mysqli_query($link,$query);
$row  = mysqli_fetch_array($result);
$nomor = $row['noMax'];
    //ambil karakter nomor urut surat paling kiri sebanyak 1 karakter (0, 1)
$noUrut = (int) substr($nomor, 0, 1);
$noUrut++;

//mengambil bulan dan tahun dari tanggal surat
$bulan = substr($tgl_surat,3,2);
$tahun = substr($tgl_surat,6,4);

//membuat nomor surat
$nomor_surat = $noUrut."/SK/".$bulan."/DPK3/".$tahun;

the output will be

1/SK/06/DPK3/2014.

the first string (1) will add every time I insert data. I have tried until 10/SK/06/DPK3/2014, but when I continued to input, the output still

10/SK/06/DPK3/2014, not 11/SK/06/DPK3/2014.

is there another way? thank you for every responses and answers.

  • 写回答

2条回答 默认 最新

  • douxuan0698 2014-06-22 11:32
    关注

    Problem is on line with $noUrut = (int) substr($nomor, 0, 1); where you take alwazy the first character (when string begins with 10, you take 1only and 1++ makes 2.

    So, you need to find first slash (/) position and than use substr.

    $noUrut = (int)substr($nomor, 0, strpos($nomor, '/'));
    $noUrut++;
    

    OR just oneline

    $noUrut = (int)substr($nomor, 0, strpos($nomor, '/')) + 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗