-1

I am new to PHP and working on an assignment. I'm almost finished but feel like I'm beating my head against the wall with this last step.

The user chooses a survey and then answers 10 questions. After the 10th question is answered, they are redirected to the results.php page which should display their questions and answers.

I know that I need to use a foreach loop but this is my first time working with them. I've finished multiple sites to get an understanding of how they work but the examples are not helping me to understand how to make it work in my assignment.

Right now I get this when I run the file:

Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH), expecting end of file in C:\xampp\htdocs\sandbox\HW4\results.php on line 38

When I remove the endforeach, I'm told that $answer within the echo is undefined.

I've run out of ways to change my loop. Nothing works. Some help would be appreciated. I apologize if I include code that is not relevant to my problem.

With little understanding of PHP, I'm not quite sure what I need to include and what I don't. I'm including code from my survey.php file that includes the array session and my code from the results file that should contain my foreach loop.

Thank you for your time.

$isPostBack = filter_input(INPUT_POST, 'submitButton') !== NULL;
if ($isPostBack) {
    // This is what happens if there is a postback.
    $choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_INT);
    if ($choose_survey !== NULL) {
        // Check the value of $choose_survey and then set 'survey' accordingly, e.g.
        // These numbers are the keys to the arrays with the list of surveys
        switch ($choose_survey) {
            case 0:
                $_SESSION['survey'] = $survey0;
                break;
            case 1:
                $_SESSION['survey'] = $survey1;
                break;
            case 2:
                $_SESSION['survey'] = $survey2;
                break;
            default:
                break;
        }

        $_SESSION['answers'] = array();
        $_SESSION['number'] = 1;
    } else {
        // A survey is not selected because it was already chosen.
        // get the value from the radio button.
        $answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
        if ($answer == NULL) {
            echo '<p>Please select an answer before clicking Submit.</p>';
        } else {
            $question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_INT);
            $question = $_SESSION['survey'][$question_key];
            // this will be used later to display the answers/results
            $_SESSION['answers'][$question] = $answer;
            unset($_SESSION['survey'][$question_key]);
            // This is adding 1 to the question number unless session  number is 10
            if ($_SESSION['number'] == 10) { // if = to 10 then we redirect to next page.
                header('Location: results.php');
            } else { // If number is less than 10, we add 1
                $_SESSION['number'] += 1;
            }
        }
    }
} else {
    // This is what happens if there is no postback.
}
?>

results.php file

<br /> <p>Thank you for participating in the survey!</p>
<html lang="en">
<head>
    <title>Survey Results</title>
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
    <form action="logout.php">
        <p>Here are your results</p>
        <input type="submit" id="submit" value="Logout" />
    </form>
        <section>
            <?php foreach ($_SESSION['answers'] as $question => $answer) ?>
            <p>
                <?php echo "$answer <br/>"; ?>
            </p>
         <?php endforeach; ?>
        </section>
</body>
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Vienne
  • 101
  • 6
  • You might want to explain what your actual _issue_ is? What happens when you run your code? Error messages? Wrong result? What does `$_SESSION['answers']` contain? Do a `var_dump($_SESSION['answers']);` and check if it contains what you expect. – M. Eriksson Mar 05 '19 at 06:17
  • I'm sorry. I should have thought to include what is happening. I'll edit my post to include that right now. – Vienne Mar 05 '19 at 06:20
  • and the var_dump shows arrays(0). – Vienne Mar 05 '19 at 06:24
  • Do you have `session_start()` in the top? If not, then the sessions won't be persistent between requests. – M. Eriksson Mar 05 '19 at 06:25
  • Don't use `
    ` to style your contents; use CSS instead.
    – Raptor Mar 05 '19 at 06:26
  • @Raptor Thank you. I've removed the
    and, yes, I do have session_start() at the top of my file.
    – Vienne Mar 05 '19 at 06:35

3 Answers3

2

did you loaded the session_start() on your file? If not this may not enable to save your sessions that you are setting. Also on your results.php you can change your code to:

<?php foreach($_SESSION['answers'] as $question => $answer){ ?>
        <p>
            <?php echo $answer."<br/>"; ?>
        </p>
     <?php }
  ?>

Hope this helps!

Roshan
  • 786
  • 1
  • 9
  • 20
  • Thank you so much. It's now displaying the answers so that is progress. Just need to figure out how to get the questions there as well. Yes, I do have session_start() at the beginning of the file. When I do a var_dump now it does list the questions in the array. – Vienne Mar 05 '19 at 06:30
  • I have the questions displaying now as well. I only had to add "$question," to the echo. Geez, I have a talent for making things so much more difficult than they have to be. Thank you again for the help. – Vienne Mar 05 '19 at 06:39
  • Way to go brother! – Roshan Mar 05 '19 at 06:44
1

I am not sure, but maybe you could place <?php session_start();?> at the beginning of your results.php to make your script able to work with $_SESSION variable? Also try to var_dump($_SESSION); as in result.php, just in case.

  • Yes, I have session_start() at the beginning of the file. Thank you. The var_dump lists all the questions in the array. I made a change to my code and my page is now displaying the answers but not the questions – Vienne Mar 05 '19 at 06:33
  • To see your question, maybe you could try make foreach as this: foreach ($_SESSION['answers'] as $question => $answer); – Nikolay Zubkov Mar 05 '19 at 06:38
  • I have it working now - displaying both questions and answers. I added "$questions," in front of $answers in the echo. Thank you for your suggestions and taking the time to try to help me. It seems I can't post a question without it getting voted down. It's very discouraging when my understanding of php is still so minimal that I can't even formulate a plea for help properly. – Vienne Mar 05 '19 at 06:44
1

I surprised that nobody noticed the syntax error in the foreach.

If you want to use the endforeach style, you can change your code like this (this is called Alternative Syntax):

<section>
    <?php foreach ($_SESSION['answers'] as $question => $answer): ?>
        <p>
            <?php echo "$answer <br/>"; ?>
        </p>
    <?php endforeach; ?>
</section>

Or if you want to use PHP-way to make a foreach, you can write it like this:

<section>
    <?php foreach ($_SESSION['answers'] as $question => $answer) { ?>
        <p>
            <?php echo "$answer <br/>"; ?>
        </p>
    <?php } ?>
</section>

However, for cleaner HTML & PHP code, I suggest you to write it like this:

<section>
    <?php 
    foreach ($_SESSION['answers'] as $answer) { 
        echo "<p>$answer</p>" . PHP_EOL;
    }
    ?>
</section>

Note that I removed unused $question variable.

Raptor
  • 53,206
  • 45
  • 230
  • 366