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 trialtawanda chikwavaire
7,613 PointsUse the trim function to remove blank spaces and verify that the value is not an EMPTY word.
<?php session_start(); $word1 = htmlspecialchars($_SESSION['word'][1]); if (!isset($_SESSION['word'][1])) { header('location: /play.php?p=1'); exit; } $word2 = htmlspecialchars($_SESSION['word'][2]); if (!isset($_SESSION['word'][2])) { header('location: /play.php?p=2'); exit; } $word3 = htmlspecialchars($_SESSION['word'][3]); if (!isset($_SESSION['word'][3])) { header('location: /play.php?p=3'); exit; } $word4 = htmlspecialchars($_SESSION['word'][4]); if (!isset($_SESSION['word'][4])) { header('location: /play.php?p=4'); exit; } $word5 = htmlspecialchars($_SESSION['word'][5]); if (!isset($_SESSION['word'][5])) { header('location: /play.php?p=5'); 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';
<?php session_start();
if(empty(trim(($SESSION['word'][1])))){ header('Location: play.php?p=1'); exit; }
else if(empty(trim(($SESSION['word'][2])))){ header('Location: play.php?p=2'); exit; }
else if(empty(trim(($_SESSION['word'][3])))){ header('Location: play.php?p=3'); exit; }
else if(empty(trim(($SESSION['word'][4])))){ header('Location: play.php?p=4'); exit; }
else if(empty(trim(($SESSION['word'][5])))){ header('Location: play.php?p=5'); exit; }
$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';
2 Answers
Patrik Horváth
11,110 PointsThere is a lot of solutions here is my :)
<?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]);
if (isset($_SESSION['word'][1]) && isset($_SESSION['word'][2]) && isset($_SESSION['word'][3]) && isset($_SESSION['word'][4]) && isset($_SESSION['word'][5]) ) {
for($i = 1; $i <= 5; $i++) {
if (strlen($str) > 0 && strlen(trim($str)) == 0) {
header('Location: play.php?p=');
}
$_SESSION['word'][i] = str_replace(' ', '', $string);
empty($_SESSION['word'][i]);
}
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';
} else {
if (empty($_SESSION['word'][1])) {
header('Location: play.php?p=1');
} else if (empty($_SESSION['word'][2])) {
header('Location: play.php?$p=2');
} else if (empty($_SESSION['word'][3])) {
header('Location: play.php?p=3');
} else if (empty($_SESSION['word'][4])) {
header('Location: play.php?p=4');
} else if (empty($_SESSION['word'][5])){
header('Location: play.php?p=5');
}
}
Daniel LittleThunder
6,975 PointsThat's a lot of code to go through for a task. If you set a variable in your for loop in the previous exercise, all you have to do is add a second argument to the conditional in order to also check for the trim. The code below worked for me and simply built off of the previous code.
for ($s = 1; $s <= 5; $s++) {
if (empty($_SESSION['word'][$s]) && trim($_SESSION['word'][$s] == FALSE)) {
header("location:story.php?p=$s");
exit;
}
}
I hope this helped. Another pointer is that in the first exercise, the !isset handling function worked for me inside of the conditional, but in the second one I had to change it to the empty function to pass. I'm not sure why.