0

下面我有一行代码,它说明了用户当前在总会话数中的哪个会话。

<h1>CREATING QUESTIONS AND ANSWERS: SESSION (AAA) <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>

因此,例如,该行可以读取:

CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 3 OF 3

现在我遇到的问题是,假设我想要 3 个会话,它应该是这样的:

第一次用户打开页面:CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 1 OF 3

但相反,它是这样读的:CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 3 OF 3

如果假设我想要 7 个会话,则相同,对于第一个会话,它应该是:CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 1 OF 7但它的读取方式是这样的:CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 7 OF 7.

所以我的问题是,我怎样才能让第一个会话等于 1...

以下是当前代码:

if(isset($_POST['sessionNum'])){
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = $_POST['sessionNum'];

    }

if (!isset($_SESSION['sessionCount'])) {
    $_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    ++$_SESSION['sessionCount'];
}


$sessionMinus =  $_SESSION['sessionCount'];

更新:

以下是之前的尝试,但它一直说 2 OF 4 或 2 OF 5 或 2 OF 2 等,但没有奏效:

if(isset($_POST['sessionNum'])){
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = $_POST['sessionNum'];
            $_SESSION['sessionCount'] = 0;

    }

else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
}


$sessionMinus =  $_SESSION['sessionCount'];
4

1 回答 1

1

替换这一行

++$_SESSION['sessionCount'];

有了这个

$_SESSION['sessionCount']++;

if(isset($_POST['sessionNum'])){
    //Declare my counter for the first time
    $_SESSION['sessionCount'] = 0;
    $_SESSION['sessionNum'] = $_POST['sessionNum'];
}

if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
}
于 2012-07-03T14:02:52.860 回答