下面我有一行代码,用于说明用户当前所在的会话数与总会话数之间的关系。
<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.
所以我的问题是如何让第一个会话等于......
以下是当前代码:
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'];
答案 0 :(得分: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']++;
}