Add values to an array after isset()

I'm trying to add elements to an array after subsequent trials, but so far only one value is being added to the array. I've Googled and searched stackoverflow, and I seem to be getting only half the picture unless if I'm implementing it wrong.

There are about 40 files, which will be needed to be submited one after another, and then a value from each trial is stored in the database.

So far, this is what I've done.

$_SESSION['task2'] = array();

//Submit Trial 1
if (isset($_POST['submit_task_01'])) {

$trial1_ac_sec = cleanInput($_POST['clockInputTask_01ac']);
$trial1_est_sec = cleanInput($_POST['clockInputTask_01']);
$trial1_ac = round(($trial1_ac_sec * 42.67), 2);
$trial1_est = round(($trial1_est_sec * 42.67), 2);
$trial1_judgErr = $trial1_ac - $trial1_est;

$trial_1error = round($trial1_judgErr, 2);
array_push($_SESSION['task2'],$trial_1error);
header("location: Trial_2.php");
 }

 //Submit Trial 2
 if (isset($_POST['submit_task_02'])) {

$trial2_ac_sec = cleanInput($_POST['clockInputTask_02ac']);
$trial2_est_sec = cleanInput($_POST['clockInputTask_02']);
$trial2_ac = round(($trial2_ac_sec * 42.67), 2);
$trial2_est = round(($trial2_est_sec * 42.67), 2);
$trial2_judgErr = $trial2_ac - $trial2_est;

$trial_2error = round($trial2_judgErr, 2);
array_push($_SESSION['task2'],$trial_2error);

header("location: newEmptyPHPWebPage.php");
}

... and so on.. up until 40

I'm just wondering what am I doing wrong, I know that each time isset() will reload the page, and the previous data won't be available, so in that sense I thought I'd create an array for sessions and then push data in the session, however that doesn't seem to work.

If anyone has any ideas on what I can do, I'll greatly appreciate it. Thank You.

Answers


You type at the top of your code :

$_SESSION['task2'] = array();

You should use instead :

if (!isset($_SESSION['task2'])) {
    $_SESSION['task2'] = array();
}

Else, you clean it each time you load your page!


As far as I can see, there's a huge potential for automation here. Instead of writing 40 Blocks of this you could simply do a for-Loop and go from there. It would also be interesting to know which of the 40 values is added to the Array (I have to ask it here because for some reason StackOverflow is not allowing me to post comments. Here's my assumed solution anyway:

    // If no Session found, start it.
    if (!isset($_SESSION)) {
        session_start();
    }

if(!isset($_SESSION['task2']) {
    $_SESSION['task2'] = array();
}

    // For 40 times
    for ($i = 0; $i < 40; $i++) {

        // If i is smaller then ten, prepend 0
        if ($i < 10) {
            $task = '0' . $i;
        } else {
            $task = $i;
        }

        // if current task is set
        if (isset($_POST['submit_task_' . $task])) {
            $trial_ac_sec = cleanInput($_POST['clockInputTask_' . $task . 'ac']);
            $trial_est_sec = cleanInput($_POST['clockInputTask_' . $task]);
            $trial_ac = round(($trial_ac_sec * 42.67), 2);
            $trial_est = round(($trial_est_sec * 42.67), 2);
            $trial_judgErr = $trial_ac - $trial_est;

            $trial_error = round($trial_judgErr, 2);
            array_push($_SESSION['task2'], $trial_error);

            getCorrectPage($i);
        }
    }

    function getCorrectPage($task) {
        // Look what's in task, go to the case that matches
        switch ($task) {
            case 1 :
                header("location: Trial_2.php");
                break;
            case 2 :
                header("location: newEmptyPHPWebPage.php");
                break;
            case 3 :
                header("location: Trial_3.php");
                break;
            // AND SO FORTH...
            default :
                echo 'No Correct Page for task ' . $task;
                break;
        }

    }

Need Your Help

Relational database design: standard row values in one table vs. separate tables

database database-design relational-database

Note: I've seen a few related question about similar issues; however, none of them would fully answer my question.

Force to clear memory

java memory guava out-of-memory concurrenthashmap

I am running into some memory issues with my project, so I decided to stress test some portions to see some performance measurements. I am using Google's ConcurrentLinkedHashMap library as an LRU m...

500Hz or higher serial port data recording

windows visual-c++ serial-port embedded

Hello I'm trying to read some data from the serial port and record it in the hard drive. I'm using visual C++ express, and made an application using the windows form.