douqian4411 2015-12-01 16:24
浏览 44

如何通过循环从1月更改或空数据库开始更改自动增量

I have tried to reset a var $potong to start from 1 again when date = '01' and database table is empty.

Here is my code :

$month = date('n');
$b=romanNumerals($month);
$years=date('Y');
echo"<div class='form-group'>";
$tanggal=date('d'); 
$s=mysql_query('SELECT * FROM preorder ORDER BY id_po DESC LIMIT 1');

while($a=mysql_fetch_array($s)){
    $nomor = $a[2];
    $potong = (int)substr($nomor,0,3);
    $potong++;
    $awal =1;
}

$kosong= mysql_num_rows($s);
if($potong == 0 || $tanggal == 01){
    echo"<input type='text' class='form-control input-lg' placeholder='' name='no_po' Readonly = true value='".sprintf('%03s',$awal)."/$b/CMT/$years'>";
}
else{   
    echo"<input type='text' class='form-control input-lg' placeholder='' name='no_po' Readonly = true value='".sprintf('%03s',$potong)."/$b/CMT/$years'>";
}

I need suggestions, how to I can create this condition.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douye2036 2015-12-01 17:32
    关注

    AUTO_INCREMENT is meant for use with keys. If you want a value to start over, you will have to keep track yourself. If you have a really specific table with an AUTO_INCREMENT column set as key for month only:

    CREATE TABLE myMonth(
    month_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
    

    Then you could use the modulus/remainder:

    month=month_id%12+1;
    

    Which will return the month for you (the +1 is to correct for the fact that the modulus will remainder of value-ROUND_DOWN(value/12)*12, which will be zero if the value is a multiple of 12, and a calendar has 1 as the first month (Pretty much except in Java where somebody decided that 0 was a nice number of month))

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题