开发者

Problem with numbers and nonexistence

So I have a script that includes forms that are stored in files named like 0101, 0102, and the next category would be 0201, 0202, 0203. The first two set of numbers are the category, second set are the pages in for the forms.

The problem I am having with this numbering is that I am storing the number as 0101 or 0205开发者_如何学JAVA and since the first number is only a non-existent thing, 0, it seems to be knocking it off. The only way I managed to actually make it work in some messy way was to do something like this:

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

Where $nextcat might be 203. It doesn't seem to like the 0 in front of anything, and must be stored in a string which has something before it (in the case above, you have a "/" sign).

How do I solve the problem with data loss? I did try looking it up in other places, but I didn't know what to put in for the query.

EDIT: More code.

The number is originally stored in $_GET['content'], passed to 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;
    }
}

Hopefully that is more information needed. It looks like a mess because I tried my hardest to keep those zeros, but they keep disappearing.


You can zero-pad with sprintf:

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

This will give you:

$string = '0102';

Or if you have:

$value = 102;

Then:

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


Maybe you should use str_pad to zero-pad category and page.

    $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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜