在每次调用操作时,生成的数字都会增加,我遇到了一个问题。
当数值到达10时,我无法打印11,等等.
    $this->sql = "SELECT codice FROM accettazione ORDER BY id DESC LIMIT 1";
    if ($result = $this->hookUp->query($this->sql)) {
        if ($result->num_rows > 0) {
            while ($rw = mysqli_fetch_array($result, MYSQLI_BOTH)) {
                (int)$codice = substr($rw['codice'], -1);
                if ($codice < 9) {
                    $rw['codice'] = date("Y") . ".0000";
                } else /*if ($codice == 9 || $codice < 99)*/ {
                    $rw['codice'] = date("Y") . ".000";
                }
                echo $rw['codice'] . ++$codice;
            }
        } else {
            echo date("Y") . ".00001";
        }
    } else if ($this->hookUp->query($this->sql) === false) {
        echo "fail";
    }
}它的目标是打印值低于10的00001,低于100的++00010,低于1000的++00100,等等。
当代码为00010,值为00011时,该值返回到00001 (如果($结果->num_row> 0)不再满足)。
在mySQL表中,$rw"codice“是一个VARCHAR。
为什么?
发布于 2015-08-11 11:07:36
我想你希望用前导零显示你的值。这就是:
     1  displayed as 00001
     9  displayed as 00009
    11  displayed as 00011
  1234  displayed as 01234这是一个与IBM 029键盘机一样古老的需求。(如果我猜错了,请编辑你的问题。)
使用斯普林特在php中很容易。
$string_codice = sprintf('%05d', $codice); 编辑
我觉得你这么做很难。看起来,您需要一个字符串来读取2015.00001或2015.09876或类似的内容。但我对从代码中猜测您的需求并不自信。首先,我不知道数据库表中codice列中的值是什么样子的。
也许你可以试试这样的方法。
   /* assume the counter starts at zero */
   $codice = 0;
   if ($result->num_rows > 0) {
        /* fetch the counter value from the table if nay. */
        while ($rw = mysqli_fetch_array($result, MYSQLI_BOTH)) {
            (int)$codice = $rw['codice'];  /* no substr */
        }
    }
    /* add one to the value and format it YYYY.000nn */
    $codice++;
    $stringval = date("Y") . "." . sprintf('%05d', $codice); 
    echo $stringval;https://stackoverflow.com/questions/31939694
复制相似问题