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

use values(features) in a vector to calculate cosine similarity for opencv

c++ image opencv vector cosine-similarity

I am recently working on a project where in I have extracted some features regarding an image, and want to find if there are any similarities between two images using those features.

Dynamically create and populate table in Android app

c# android xamarin

I'm wondering how to dynamically create and populate a TableLayout in Android. I can create the table beforehand statically (via XML) but I need to be able to populate it with a dataset retrieved f...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.