Jenna's Mad lib Generator
Demonstrates custom functions that creates and formats a story.
Demonstrates custom functions that creates and formats a story.
<?php
include('../includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo("<h1>Jenna's Mad lib Generator</h1>");
echo("<p>Demonstrates custom functions that creates and formats a story.</p>");
// initialize variables
$adjective = '';
$noun = '';
$verb = '';
$place = '';
$storyTitle = '';
$story = '';
$formattedStory = '';
$storyBox = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$adjective = htmlspecialchars(strip_tags(trim($_POST['adjective'])));
$noun = htmlspecialchars(strip_tags(trim($_POST['noun'])));
$verb = htmlspecialchars(strip_tags(trim($_POST['verb'])));
$place = htmlspecialchars(strip_tags(trim($_POST['place'])));
if ($adjective == '' || $noun == '' || $verb == '' || $place == '') {
echo("<p>Please fill out all the fields to create a story</p>");
}
else
{
$storyTitle = createStoryTitle($place);
$story = buildStory($adjective, $noun, $verb, $place);
$formattedStory = formatStoryText($storyTitle, $story);
$storyBox = displayStoryBox($storyTitle, $formattedStory);
echo("<h2>Function Results</h2>");
echo("<h3>Original Story</h3>");
echo("<p><strong>$storyTitle</strong><br><br>$story</p>");
echo("<h3>Formatted Story</h3>");
echo($formattedStory);
echo("<h3>Final Displayed Story</h3>");
echo($storyBox);
}
}
?>
<hr>
<h2>Create A Story!</h2>
<form action="" method="post">
<p>Adjective:<br>
<input type="text" name="adjective" value="<?php echo $adjective; ?>"></p>
<p>Noun:<br>
<input type="text" name="noun" value="<?php echo $noun; ?>"></p>
<p>Verb:<br>
<input type="text" name="verb" value="<?php echo $verb; ?>"></p>
<p>Place:<br>
<input type="text" name="place" value="<?php echo $place; ?>"></p>
<p><input type="submit" value="Create Story!"></p>
</form>
<?php
//create title
function createStoryTitle($place){
if(strlen($place) == 0){
$place = "Unknown Place";
}
$title = "Adventure at the " . ucwords($place);
return $title;
}
function buildStory($adjective, $noun, $verb, $place){
$story = "One night, a(n) $adjective $noun decided to $verb all the way through the $place. "
. " Everyone who saw the $noun could not believe how $adjective it looked"
. " while it continued to $verb.";
return $story;
}
//format story by ensuring the sentence displays correctly
function formatStoryText($title, $story){
$story = trim($story);
$formattedStory = "<p style='font-family: \"Comic Sans MS\", \"Comic Sans\", cursive; color: #8b0000;'>";
$formattedStory .= "<strong>Your Mad Libs Story:</strong><br><br><strong>$title</strong><br><br>$story</p>";
return $formattedStory;
}
function displayStoryBox($title, $story){
$html = "<div style='border: 2px solid #0354c2; padding: 15px; margin-top: 10px; margin-left: auto; margin-right: auto; background-color: #eef4ff; width: 70%;'>";
$html .= $story;
$html .= "</div>";
return $html;
}
include('../includes/footer.php');
?>