0

运行提交代码后,它应该执行以下代码:

session_start();

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

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
            $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
 }

$sessionMinus =  $_SESSION['sessionCount'];

当我回显下面的代码时:

<?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?>

每次点击提交按钮时,如果 for 的数量$sessionMinus小于 number $_SESSION['initial_count'],则$sessionMinus每次页面提交时它的数量应该加 1。

但它根本没有将数字加 1。$sessionMinus每次提交页面时都会一直输出“1”。我需要在我的代码中更改什么?

我执行了一个var_dump($_SESSION);,这就是它输出的内容:

array(3) { 
   ["sessionCount"]=> int(1) 
   ["initial_count"]=> string(1) "5" 
   ["sessionNum"]=> int(5) 
}
4

2 回答 2

0

阅读您的评论后,我认为这就是您所追求的:

if(!isset($_SESSION['sessionCount']) || !$_SESSION['sessionCount'] ) {
    //Declare my counter for the first time

    $_SESSION['initial_count'] = $_POST['sessionNum'];
    $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
    $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
 }

$sessionMinus =  $_SESSION['sessionCount'];
于 2012-09-16T12:55:40.703 回答
0

调试它如下:

session_start();

var_dump( $_POST, $_SESSION ); //See that the post and session parameters you think should be there, really are there.
if(isset($_POST['sessionNum'])) {
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
            $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    var_dump( $_SESSION['sessionCount'] ); //check the current count (and that you really are here.
    $_SESSION['sessionCount']++;
    var_dump( $_SESSION['sessionCount'] ); // Check that the count is updated correctly.
 }

$sessionMinus =  $_SESSION['sessionCount'];
于 2012-09-16T13:25:44.743 回答