user1292857
user1292857

Reputation: 27

How to add up one by one

I have a variable known as "$SessionMinus". What I want to do is that everytime the form is submitted or refreshed, It will add the number by 1 each time so it will start off as 1, then 2, then 3... all the up to the highest number which is "$_SESSION['sessionNum']. How can this be done?

Thanks

    <?php
    session_start();

                $sessionMinus = 1;

        if(isset($_POST['sessionNum'])){

            //Declare my counter for the first time
               if ($sessionMinus < $_SESSION['sessionNum'])
    {
        $sessionMinus++;
    }

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

    }


?>


<body>

<?php

echo $sessionMinus;

?>

      <form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >
        <p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" /></p>
        </form> 


    <?php

    $outputDetails = "";
    $outputDetails .= "
    <table id='sessionDetails' border='1'>
    <tr>
    <th>Number of Sessions:</th> 
    <th>{$_SESSION['initial_count']}</th>
    </tr>";
    $outputDetails .= "        </table>";

    echo $outputDetails;


    ?> 


</body>

Upvotes: -1

Views: 121

Answers (2)

D_C
D_C

Reputation: 946

It seems like you are trying to keep track of how many times the session has started, up to a max amount of time. Create a count session variable and initially assign it one. Every time session is loaded, increment then assign this new variable to 'sessionMinus'

 <?php
    session_start();


    if(isset($_POST['sessionNum'])){
         $_SESSION['sessionNum'] = $_POST['sessionNum'];
    }

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

    $sessionMinus =  $_SESSION['sessionCount'];


?>

EDIT: made a small mistake on the second isset... corrected it

Upvotes: 0

user1269636
user1269636

Reputation:

If I understand you correctly, you just have to increment $sessionMinus++; in your first condition ?

if(isset($_POST['sessionNum']))
{
    if ($sessionMinus < $_SESSION['sessionNum'])
    {
        $sessionMinus++;
    }
}

This way, everythime someone will submit the form (when $_POST['sessionNum'] is defined), $sessionMinus will be incremented.

Upvotes: 2

Related Questions