数字和不存在的问题


Problem with numbers and nonexistence

因此,我有一个脚本,其中包含存储在名为01010102的文件中的表单,下一个类别将是0201、0202、0203。前两组数字是类别,第二组是表格中的页面。

我对这个编号的问题是,我将数字存储为0101或0205,由于第一个数字只是一个不存在的东西,0,它似乎在把它敲掉。我设法使它以某种混乱的方式工作的唯一方法是做这样的事情:

"planpages/0" . $nextcat . ".php"

其中$nextcat可能是203。它似乎不喜欢任何东西前面的0,必须存储在一个前面有东西的字符串中(在上面的例子中,你有一个"/"号(。

如何解决数据丢失的问题?我确实试过在其他地方查找它,但我不知道该为查询输入什么。

编辑:更多代码。

该编号最初存储在$_GET['content']中,然后传递给nextForm($current)

function nextForm($current) {
    $next = $current[0] . $current[1] . $current[2] . $current[3] + 1;
    $nextcat = $current[0] . $current[1] + 1 . 0 . 1;
    if(file_exists("planpages/0" . $next . ".php")) {
        return "0" . $next;
    } elseif(file_exists("planpages/0" . $nextcat . ".php")) {
        return "0" . $nextcat;
    } else {
        return $current;
    }
}

希望这是所需要的更多信息。这看起来一团糟,因为我尽了最大的努力来保留那些零,但它们一直在消失。

您可以使用sprintf:将pad清零

$form = 1;
$page = 2;
$string = sprintf('%02d%02d', $form, $page);

这将给你:

$string = '0102';

或者如果你有:

$value = 102;

然后:

$string = sprintf('%04d', $value);

也许您应该使用str_pad来归零焊盘类别和页面。

    $category = 2;
    $pages = 1;
    $cat = str_pad($category, 2, "0", STR_PAD_LEFT);
    $pag = str_pad($pages, 2, "0", STR_PAD_LEFT);
    $filename = $cat . $pag;
    // $filename = "0201"