Veronica's lucky number generator
Good Afternoon!
 
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Veronica\'s lucky number generator</h1>');
// variables being passed via the POST ternary operators
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$letter = isset($_POST['letter']) ? $_POST['letter'] : '0';
// filter all input taken from the Browser
$step = htmlentities($step);
$letter = htmlentities($letter);
$hour = date('G'); // 'G' returns 24-hour format of an hour with leading zeros
$j = 0;
if ($step == 0) // first time into this form
{
switch ($hour)
{
case ($hour >= 0 && $hour < 12):
echo('<H3>Good Morning!</h3>');
break;
case ($hour >= 12 && $hour < 18):
echo('<H3>Good Afternoon!</h3>');
break;
case ($hour >= 18 && $hour < 22):
echo('<H3>Good Evening!</h3>');
break;
case ($hour >= 22 && $hour <= 24):
echo('<H3>Good Night!</h3>');
break;
default:
echo('<H3>Greetings!</h3>');
break;
}
?>
<form action="decisions_loops_form.php" method="POST">
<label for="name">First Letter of Your Name</label>
<input type="text" size="24" id="name" name="letter" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else // else show the results
{
// get lucky number
for ($i = 'a'; $i <= strtolower($letter); $i++)
{
$j = $j + 1;
}
// get fortune
$luck = ($j < 8) ? "Today is your lucky day!" : "Maybe tomorrow will be your lucky day";
echo("<h3>Your letter = $letter</h3>");
echo("<h4>Your lucky number = $j</h4><br>");
echo("<h4>Your fortune: $luck</h4><br>");
// provide a fresh form to try again
?>
<form action="decisions_loops_form.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>