Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrew mwendo
5,712 Pointsplease help Check that each of the required session variables are not set before attempting to write the stor
Challenge Task 1 of 2
Check that each of the required session variables are not set before attempting to write the story. If the session variables are not set, redirect to play.php with the p parameter set to the first missing session variable.
<?php
session_start();
$word1 = htmlspecialchars($_SESSION['word'][1]);
$word2 = htmlspecialchars($_SESSION['word'][2]);
$word3 = htmlspecialchars($_SESSION['word'][3]);
$word4 = htmlspecialchars($_SESSION['word'][4]);
$word5 = htmlspecialchars($_SESSION['word'][5]);
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1;
echo ' programmer named ' . $word2;
echo '. </p>';
echo '<p>This ' . $word3;
echo ' programmer used Treehouse to learn to ' . $word4;
echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>';
echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>';
echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
6 Answers
Ivan Bagaric
Courses Plus Student 12,356 PointsSomething like this maybe
for ($i = 1; $i <= 5; $i++) {
$tmp = trim($_SESSION['word'][$i]);
if (empty($tmp)) {
if ($i == 1) {
header('location:play.php');
exit;
} else {
header('location:play.php?p='.$i);
exit;
}
}
}
Ivan Bagaric
Courses Plus Student 12,356 Pointsfor ($i = 1; $i <= 5; $i++) {
if (!isset($_SESSION['word'][$i])) {
if ($i == 1) {
header('location:play.php');
exit;
} else {
header('location:play.php?p='.$i);
exit;
}
}
}
Andrew mwendo
5,712 Pointshow do I continue to the next challenge with the same code
Jeremy Antoine
15,785 PointsThis answer only applies to the first part of the challenge. No else statement required for this step of the challenge.
for ($word =1; $word <=5; $word++) {
if(!isset($_SESSION['word'][$word])) {
header('location:play.php?p=' . $word);
exit;
}
}
Prince Mandaza
2,943 Points<?php session_start(); $word1 = htmlspecialchars($_SESSION['word'][1]); $word2 = htmlspecialchars($_SESSION['word'][2]); $word3 = htmlspecialchars($_SESSION['word'][3]); $word4 = htmlspecialchars($_SESSION['word'][4]); $word5 = htmlspecialchars($_SESSION['word'][5]);
for ($i = 1; $i <= 5; $i++) { $tmp = trim($_SESSION['word'][$i]); if (empty($tmp)) { if ($i == 1) { header('location:play.php'); exit; } else { header('location:play.php?p='.$i); exit; } } }
include 'inc/header.php';
echo '<h1>My Treehouse Story</h1>';
echo '<p>There once was a(n) ' . $word1; echo ' programmer named ' . $word2; echo '. </p>'; echo '<p>This ' . $word3; echo ' programmer used Treehouse to learn to ' . $word4; echo ' the ' . $word5 . '.</p>';
echo ' <a class="btn btn-default btn-lg" href="#" role="button">Save Story</a>'; echo ' <a class="btn btn-default btn-lg" href="play.php" role="button">Play Again</a>'; echo ' <a class="btn btn-default btn-lg" href="index.php" role="button">Other Stories</a>';
include 'inc/footer.php';
Mohammed Abdelhadi
17,331 Pointsfor($i = 1; $i <=5 ; $i++) { if (empty($_SESSION['word'][$i])) { header("Location: play.php?p=$i"); break; } }
Craig Maclean
2,051 PointsThis was my code which worked:
for($i = 1; $i <= 5; $i++) {
if(!isset($_SESSION['word'][$i])) {
header("Location: play.php?p=" . $i);
exit;
}
}
Andrew mwendo
5,712 PointsAndrew mwendo
5,712 Pointsyou are the best