Scripts
Webrekenmachine
Mijn allereerste script.. Ik heb me even bezig gehouden met php 5(boek) en kwam op dit uit een online rekenmachine. Hij werkt zowel onder 0 als zoveel cijfers achter de komma..Hij is beveiligd tegen intypen van cijfers op de display door een 'hidden'. Geniet van mijn eerste script. Officiƫle Author Zie boek PHP5 en Mysql de basis ..(voor die lieve jonge die zich daarover druk maakte :s)
webrekenmachine
[code]
<?php
error_reporting(E_ALL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>PHP rekenmachine</title>
<link rel="stylesheet" type="text/css" href="rekenmachine.css" />
</head>
<body>
<?php
$calc = new Calculator();
$calc->checkAction();
$calc->display();
?>
</body>
</html>
<?php
class Calculator {
protected $buttons = array ();
protected $totals;
protected $ops = array ('div' => '/', 'mult' => '*', 'sub' => '-', 'add' => '+');
public function __construct() {
$this->totals = new Totals();
reset($this->ops);
for ($i = 1; $i < 10; $i ++) {
$this->buttons[] = new NumberButton("b$i", "$i");
if ($i % 3 == 0) {
list ($key, $val) = each($this->ops);
$this->buttons[] = new OpsButton("$key", "$val");
}
}
$this->buttons[] = new NumberButton("b0", "0");
$this->buttons[] = new NumberButton("dot", ".");
$this->buttons[] = new EqualsButton("eq", "=");
list ($key, $val) = each($this->ops);
$this->buttons[] = new OpsButton("$key", "$val");
$this->buttons[] = new ClearButton("clear", "clear");
}
public function display() {
$srv = $_SERVER['PHP_SELF'];
echo "<div class='rekenmachine''>\n";
echo '<br/><br/>';
echo "<form action='$srv' method='post'>\n";
echo $this->totals->displayTotals();
echo '<br/><table><tr>';
$i=0;
foreach ($this->buttons as $button) {
echo $button->displayButton();
$i++;
if ($i % 4 == 0) {
echo '</tr></tr>'; // start a new row after 4 buttons
}
}
echo '</tr></table></div>';
}
public function checkAction() {
foreach ($this->buttons as $button) {
if ($button->keyIsset()) {
$button->handleIsset($this->totals);
break;
}
}
}
}
class Totals {
public $display = "";
public $expression = "";
public function __construct() {
$this->display = $_POST['display'];
$this->expression = $_POST['totval'];
}
public function clear() {
$this->display = "";
$this->expression = "";
}
public function addNumber($num) {
$this->display = $this->display.$num;
}
public function addOps($ops) {
$this->expression = $this->expression.$this->display.$ops;
$this->display = "";
}
public function evalTotals() {
$this->expression = $this->expression.$this->display;
$last = substr($this->expression, -1, 1); // find the last character
if (is_numeric($last)) {
eval ("\$this->display = $this->expression;");
$this->expression = "";
}
}
public function displayTotals() {
$dval = $this->display;
$disp = "<input type='text' name='display' class='display'".
"value='$dval' size='14' maxlength='40' readonly />";
$expression = $this->expression;
$hidden = "<input type='hidden' name='totval' value='$expression' />";
return "$disp\n$hidden<br/>";
}
}
abstract class CalcButton {
protected $buttonName;
protected $buttonValue;
public function __construct($name, $value) {
$this->buttonName = $name;
$this->buttonValue = $value;
}
public function displayButton() {
$buttonValue = $this->buttonValue;
$buttonName = $this->buttonName;
return "<td><input type='submit' name='$buttonName' value='$buttonValue'/></td>\n";
}
public function keyIsset() {
if (isset($_POST["$this->buttonName"])) return TRUE;
else return FALSE;
}
abstract public function handleIsset(Totals $totals);
}
class NumberButton extends CalcButton {
public function handleIsset(Totals $totals) {
$totals->addNumber($this->buttonValue);
}
}
class OpsButton extends CalcButton {
public function handleIsset(Totals $totals) {
$totals->addOps($this->buttonValue);
}
}
class EqualsButton extends CalcButton {
public function handleIsset(Totals $totals) {
$totals->evalTotals();
}
}
class ClearButton extends CalcButton {
public function displayButton() {
$buttonValue = $this->buttonValue;
$buttonName = $this->buttonName;
return "<td colspan='4'><input class='clear' type='submit' name='$buttonName' value='$buttonValue'/></td>\n";
}
public function handleIsset(Totals $totals) {
$totals->clear();
}
}
?>
[/code]
Nu de CSS :
[code]
body {background-color: white; text-align: center; }
input { font-family: "Courier New"; font-size: 30px}
table {text-align: center; margin-left: auto; margin-right: auto;}
div.rekenmachine {
width: 250px; border-style: solid; border-width: 1px; padding: 10px;
}
.display {text-align: right; font-size: 20px }
.clear { font-size: 20px }
[/code]
Ik hoop dat jullie het een erg goede eerste script vinden..
Bedankt
Ohja editje het script werkt volledig er zitten geen bugs meer in..
Als ik tijd genoeg heb post ik ook nog een soort van imagelibary .!
Edit: Verplaatst naar andere server (@Boaz)
Reacties
0