The processing and input page:
Code: Select all
<?
// Start session
session_start();
// If user has clicked the button to clear session, do so
if($_GET['session'] == 'delete') {
session_destroy(); // Delete session
session_start(); // Start fresh session
}
// Set variables
if(isset($_POST['submit'])) {
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
if(is_numeric($a) && is_numeric($b) && is_numeric($c)) {
$a = round($a);
$b = round($b);
$c = round($c);
$a = intval($a);
$b = intval($b);
$c = intval($c);
}
}
else {
$a = 'a';
$b = 'b';
$c = 'c';
}
// Process
if(isset($_POST['submit'])) {
$equation = $a.'<i>x</i><sup>2</sup>+'.$b.'<i>x</i>+'.$c;
$save = true; // Denotes that this equation should be saved in session
if((!is_int($a)) || (!is_int($b)) || (!is_int($c))) {
$ans = 'One or more of your inputs were not numbers.<br /><br />I pity da fool.';
$save = false; // Denotes that his equation should NOT be saved in session
}
else {
$disc = pow($b, 2) - (4 * $a * $c);
$root = sqrt($disc);
if(floor($root) == $root) {
$ans = 'Hoorah! That expression can be factored!';
}
else {
$ans = 'Sorry! That expression cannot be factored!';
}
}
}
?>
<html>
<head>
<title>Can it be Factored?</title>
<link href="factor.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>Can it be Factored?</h1>
</div>
<?
if(isset($ans)) {
?>
<div id="ans">
<br />
<? echo $equation.'<br /><br />'.$ans; ?>
<br /><br />
</div>
<?
}
?><div id="form">
<br />Please fill in the equation with your <i>a</i>, <i>b</i>, and <i>c</i> values.<br />Any decimals will be rounded to the nearest integer.<br /><br />
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" size="3" maxlength="5" name="a" value="a" /><i>x</i><sup>2</sup>+<input type="text" size="3" maxlength="5" name="b" value="b" /><i>x</i>+<input type="text" size="3" maxlength="5" name="c" value="c" /><br />
<input type="submit" name="submit" value="Go!" />
</form>
</div>
<?
// Make object for old answers
class oldAns {
var $oldquation;
var $oldans;
function display() {
echo '<div class="oldans">'.$this->oldquation.'<br /><br />'.$this->oldans.'</div>';
}
}
// Display old answers
if(isset($_SESSION['set'])) {
echo '<div id="oldcontainer">';
echo '<div class="close"><a href="'.$_SERVER['PHP_SELF'].'?session=delete"><font color="black">[x]</font></a></div>';
$continue = true;
$count = 0;
while($continue == true) {
if(isset($_SESSION[$count])) {
$_SESSION[$count]->display();
$count++;
}
else {
$continue = false;
}
}
echo '</div>';
}
// Save the current equation in the session
if($save == true) {
$_SESSION['set'] = true;
// Find out how many equations are saved
$continue = true;
$count = 0;
while($continue == true) {
if(isset($_SESSION[$count])) {
$count++;
}
else {
$continue = false;
$count -= 1;
}
}
while($count >= 0) {
$tempnum = $count + 1;
$_SESSION[$tempnum] = $_SESSION[$count];
$count--;
}
$_SESSION[0] = new oldAns;
$_SESSION[0]->oldquation = $equation;
$_SESSION[0]->oldans = $ans;
}
?>
</body>
</html>Code: Select all
body {
text-align: center;
background: gray;
}
a {
color: white;
}
#header {
color: white;
background: black;
width: 500px;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
padding: 5px;
}
#form {
color: white;
background: black;
width: 500px;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
padding: 5px;
}
#ans {
color: white;
background: black;
width: 500px;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
padding: 5px;
}
#oldcontainer {
color: black;
background: white;
width: 500px;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
padding: 5px;
}
.oldans {
color: white;
background: black;
width: 450px;
margin-top: 5px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
padding: 5px;
}
.close {
float: left;
text-decoration: underline overline;
}