Veronica's Word Rearranging Form
Your Message
 
<?php
include('includes/header.php');
// set error reporting to all
error_reporting(E_ALL);
echo('<h1>Veronica\'s Word Rearranging Form</h1>');
// set variables
$self = basename($_SERVER['SCRIPT_NAME']);
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$message = isset($_POST['message']) ? $_POST['message'] : '';
// filter all input for browser output
$message = htmlentities($message);
// first time into this form
if ($step == 0)
{
// set checked
$c = ' CHECKED ';
echo("Your Message ");
?>
<form action="<?php echo($self);?>" method="POST" >
<textarea cols=80 rows=6 name="message">
<?php echo($message);?>
</textarea>
<br>
<br>
<p>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</p>
</form>
<?php
} else // array work
{
echo('<br>');
echo("Mixing ~$message ~");
echo('<br><br>');
// create arrays for words
$nouns = array("Worms","Anchovies","I","Dimes","Eyes","Grinders","Lasers","Hours","You");
$verbs = array("Break","Do","Like","Are","Have","Take","Fry","Make","Were");
$others = array("From","Really","Awfully","Not","Overly","Under","At","Four","Some");
$adjectives = array("Dank","Perfect","Green","Crazy","Enlarged","Cooked","Quick","Small","Terrifying");
$objects = array("Anchors","Jokes","Zippers","Bears","Halos","Cages","Guesses","Kids","Einsteins");
$user_words = array();
$user_sorted = array();
$random_mix = array();
$mixed = array();
// check for input
$message_size = strlen($message);
// if no user input, provide surprise results only
if ($message_size < 1)
{
// set to 8, the number of elements in each random array minus 1
$size = 8;
// create a 5 word sentence
for ($i = 0; $i < 5; $i++) {
// get random index number
$r = rand(0, ($size));
// start mix up
if ($i == 0)
{
$random_mix[] = $nouns[$r] . ' ';
}
else
{
switch ($i) {
case ($i == 1):
$random_mix[] = $verbs[$r] . ' ';
break;
case ($i == 3):
$random_mix[] = $adjectives[$r] . ' ';
break;
case ($i == 4):
$random_mix[] = $objects[$r] . ' ';
break;
default: // i == 2
$random_mix[] = $others[$r] . ' ';
break;
}
}
}
} else // there is input
{
// explode the string into an array
$user_words = explode(' ', $message);
// find the length of the array
$size = count($user_words);
// start mix up
for ($i = 0; $i < $size; $i++)
{
// get random index number, add to arrays
$r = array_rand($user_words);
$r2 = rand(0,8);
$random_mix[] = $user_words[$r] . ' ';
if ($r2 % 2 == 0)
{
if ($r2 > 5)
{
$mixed[] = $user_words[$r] . ' ' . $adjectives[$r2] . ' ';
} else
{
$mixed[] = $user_words[$r] . ' ' . $objects[$r2] . ' ';
}
} else
{
if ($r2 > 4)
{
$mixed[] = $user_words[$r] . ' ' . $others[$r2] . ' ';
} else
{
$mixed[] = $user_words[$r] . ' ' . $nouns[$r2] . ' ';
}
}
}
// start sorted user array
asort($user_words);
foreach ($user_words as $word)
{
$user_sorted[] = $word . ' ';
}
}
// populate multidimensional associative array of phrases
$multi_array = array
(
array
(
'noun' => $nouns[0],
'verb' => $verbs[0],
'other' => $others[0],
'adjective' => $adjectives[0],
'object' => $objects[0],
),
array
(
'noun' => $nouns[1],
'verb' => $verbs[1],
'other' => $others[1],
'adjective' => $adjectives[1],
'object' => $objects[1],
),
array
(
'noun' => $nouns[2],
'verb' => $verbs[2],
'other' => $others[2],
'adjective' => $adjectives[2],
'object' => $objects[2],
),
array
(
'noun' => $nouns[3],
'verb' => $verbs[3],
'other' => $others[3],
'adjective' => $adjectives[3],
'object' => $objects[3],
),
array
(
'noun' => $nouns[4],
'verb' => $verbs[4],
'other' => $others[4],
'adjective' => $adjectives[4],
'object' => $objects[4],
),
array
(
'noun' => $nouns[5],
'verb' => $verbs[5],
'other' => $others[5],
'adjective' => $adjectives[5],
'object' => $objects[5],
),
array
(
'noun' => $nouns[6],
'verb' => $verbs[6],
'other' => $others[6],
'adjective' => $adjectives[6],
'object' => $objects[6],
),
array
(
'noun' => $nouns[7],
'verb' => $verbs[7],
'other' => $others[7],
'adjective' => $adjectives[7],
'object' => $objects[7],
),
array
(
'noun' => $nouns[8],
'verb' => $verbs[8],
'other' => $others[8],
'adjective' => $adjectives[8],
'object' => $objects[8],
),
);
?>
<!-- show results & provide a button to try again -->
<h3>
<?php echo("After mixing up your word array, we get: "); ?>
</h3>
<h4>
<?php
foreach ($random_mix as $value)
{
echo($value);
}
?>
</h4>
<br>
<!-- show other possible results -->
<h3>
<?php echo("After sorting your word array, we get: "); ?>
</h3>
<h4>
<?php
foreach ($user_sorted as $value)
{
echo($value);
}
?>
</h4>
<br>
<h3>
<?php echo("Oops! We mixed some of our words with yours! "); ?>
</h3>
<h4>
<?php
foreach ($mixed as $value)
{
echo($value);
}
?>
</h4>
<br>
<h3>
<?php echo("Our internal word arrays are: "); ?>
</h3>
<p>
<?php
for ($i = 0; $i < 9; $i++)
{
echo("<br>");
echo($multi_array[$i]["noun"] . " ");
echo($multi_array[$i]["verb"] . " ");
echo($multi_array[$i]["other"] . " ");
echo($multi_array[$i]["adjective"] . " ");
echo($multi_array[$i]["object"] . " ");
}
?>
</p>
<br>
<!-- show possible results sorted -->
<h3>
<?php echo("Our internal word arrays can be sorted by nouns: "); ?>
</h3>
<p>
<?php
array_multisort($nouns, $verbs, $others);
echo("Nouns: <br>");
print_r($nouns);
echo("<br> Verbs: <br>");
print_r($verbs);
echo("<br> Others: <br>");
print_r($others);
echo("<br> Adjectives: <br>");
print_r($adjectives);
echo("<br> Objects: <br>");
print_r($objects);
echo("<br>");
for ($i = 0; $i < 9; $i++)
{
echo("<br>");
echo($nouns[$i] . " ");
echo($verbs[$i] . " ");
echo($others[$i] . " ");
echo($adjectives[$i] . " ");
echo($objects[$i] . " ");
}
?>
</p>
<br>
<h3>
<?php echo("Or our internal word arrays can be sorted by adjectives: "); ?>
</h3>
<p>
<?php
array_multisort($adjectives, $nouns, $verbs);
echo("Adjectives: <br>");
print_r($adjectives);
echo("<br> Nouns: <br>");
print_r($nouns);
echo("<br> Verbs: <br>");
print_r($verbs);
echo("<br> Others: <br>");
print_r($others);
echo("<br> Objects: <br>");
print_r($objects);
echo("<br>");
for ($i = 0; $i < 9; $i++)
{
echo("<br>");
echo($adjectives[$i] . " ");
echo($nouns[$i] . " ");
echo($verbs[$i] . " ");
echo($others[$i] . " ");
echo($objects[$i] . " ");
}
?>
</p>
<br><br>
<form action="<?php echo($self);?>" method="POST" >
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>