2

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.

4
  • Remember to exit after your redirects. Commented Sep 8, 2012 at 21:38
  • @arxanas Thank you for pointing that out, totally forgot. Commented Sep 8, 2012 at 21:41
  • have you called session_start() ? Commented Sep 8, 2012 at 21:43
  • only other thing i can suggest is using [] to add elements php.net/manual/en/function.array-push.php Commented Sep 8, 2012 at 21:49

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

Comments

2

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;
        }

    }

1 Comment

Thank you, I've been trying to look for ways not to duplicate my code, and this is exactly it.
0

Tim? Just a thing I would like to point out, although your problem has been answered, it might save you a lot of copy/pasting.

Code Duplication is something programmers are very keen on avoiding it, it seems like you will copy paste this 40 times although from the two times I've seen here in your code example, they are almost identical?

I can't make out of the rest, but just wanted to point out that this could be done in a clean fashion.

Cheers and gl

1 Comment

Cheers Bud, I'll definitely remember that for next time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.