I am teaching myself OOP in PHP. This is my first script. I had some good help in an earlier post in setting up the classes. I have now taken this to the next step and completed a rough working page. The layout needs some work to make the results easier to read and I’m not keen on the colours. And I have not yet put in any text to explain what the user has to do.
But I am asking for advice re the PHP - am I going about using forms in OOP in a good way or could it be done better?
In the UK some estate agents (realtors) only quote room sizes in metres but I can visualise room sizes better in feet and inches. So this page takes as input a room name and width and length in metres, up to 2 decimal places. On clicking the submit button it outputs the room name, the metric width and length and area and the same in feet and inches. The room name and width and length are displayed as an input in the same form.
When the user submits another room they can also alter the name and/or dimensions of the old room, or to delete the old room they simply make one of the fields empty.
As you can see, the inputs to the form are sent as arrays. When submitted, it works through foreach loop on
$_POST['roomName']
and for each room it creates an object $room and carries out the above on that room. The next iteration creates a new $room and does the same again. Is this a sensible way of doing this? Once one $room object has created it’s input and conversions I don’t think there is any need to keep it and create an object with a different name?
Any advice on OOP PHP and forms gratefully received. Thank you.
<!doctype = html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheets/style.css?1">
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root{
--textColor: #039;
--headlineColor: #69e;
--backgroundColor: #fea;
--siteBackgroundColor: #ffffff;
--footerColor: darkblue;
--footerGradientTop: #06f;
--footerGradientBottom: #6cf;
--inputBorder: #999;
}
img{
display: block;
}
html {
height: 100%;
}
body{
min-height: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
background-color: var(--backgroundColor);
}
main{
margin: 0 auto;
background-color: var(--siteBackgroundColor);
width: auto;
min-width: 700px;
padding: 10px;
box-shadow: 5px 0 5px rgba(128,128,128,0.5);
}
p,span,input,legend,td,th{
font-family: roboto, sans-serif;
color: var(--textColor);
padding: 5px;
}
p.results{
white-space: nowrap;
}
legend{
font-size: smaller;
}
input.new,fieldset.new{
border-radius: 4px;
width: 100%;
}
input{
border: none;
}
fieldset{
border: inset 1px var(--inputBorder);
margin: 5px 0;
}
input.old{
border: solid 1px var(--inputBorder);
border-radius: 4px;
width: 5em;
text-align: right;
margin: 5px;
}
input.room{
width: 15em;
text-align: left;
}
input.submit{
width: auto;
border: 1px outset;
background-color: var(--backgroundColor);
box-shadow: 2px 2px 2px 0 var(--inputBorder);
}
input.submit:hover{
color: var(--backgroundColor);
background-color: var(--textColor);
border: 1px inset;
}
h1,h2,h3,h4,h5,h6{
font-family: bree, times, serif;
color: var(--headlineColor);
padding: 5px;
}
footer.footer{
background-color: var(--footerColor);
background-image: linear-gradient(to bottom, var(--footerGradientTop), var(--footerGradientBottom));
min-width: 700px;
margin: 0 auto;
font-size: smaller;
padding: 5px;
box-shadow: 5px 0 5px rgba(128,128,128,0.5);
z-index: -1;
}
footer > span{
color: white;
}
</style>
<title>
Dan's rooms calculator
</title>
</head>
<body>
<header>
</header>
<main>
<h1>
Dan's Room Calculator
</h1>
<?php
//if form has been submitted with completed fields
if (!empty($_POST['roomName']) && !empty($_POST['width']) && !empty($_POST['length'])){
//*******************************************************************
class Room {
private $width;
private $length;
private $roomName;
public function __construct(float $width, float $length, string $roomName) {
$this->width = $width;
$this->length = $length;
$this->roomName = $roomName;
}
public function getWidth(): float {
return $this->width;
}
public function getLength(): float {
return $this->length;
}
public function getArea(): float {
return $this->width * $this->length;
}
public function getName(): string {
return $this->roomName;
}
}
//*******************************************************************
class Converter {
public static function convertMetricToImperialDistance(float $metres): string {
$valInFeet = $metres * 3.2808399;
$valFeet = (int) $valInFeet;
$valInches = round(($valInFeet-$valFeet) * 12);
return $valFeet
."′ "
.$valInches
."″" ;
}
public static function convertMetricToImperialArea(float $squareMetres): string{
return round($squareMetres * 10.7639104)."ft<sup>2</sup>";
}
}
//*******************************************************************
foreach ($_POST['roomName'] as $key=>$value){
//sanitise input
$roomName = filter_var($_POST['roomName'][$key], FILTER_SANITIZE_STRING);
$width = filter_var($_POST['width'][$key], FILTER_SANITIZE_STRING);
$length = filter_var($_POST['length'][$key], FILTER_SANITIZE_STRING);
//if the user has emptied an existing room's field then do not proceed with this room and move onto the next room. Will also allow user to
if ($roomName && $width && $length){
//instantiate a new Room object
$room = new Room($width, $length, $roomName);
//Output the dimensions of the Rooms, as inputs for the form below
echo"
<p class='results'>
<input value='".$room->getName()."' name='roomName[]' class='old room' form='mainForm'>
<input value='".$room->getWidth()."' name='width[]' class='old' form='mainForm'><span>m</span><span>x</span>
<input value='".$room->getLength()."' name='length[]' class='old' form='mainForm'><span>m</span>
<span>".number_format($room->getArea(),2,'.',',')."m<sup>2</sup></span>
<span>".Converter::convertMetricToImperialDistance($room->getWidth())."</span><span>x</span>
<span>".Converter::convertMetricToImperialDistance($room->getLength())."</span>
<span>".Converter::convertMetricToImperialArea($room->getArea())."</span>
</p>
";
} //end of if ($roomName)...
} //end of foreach ($_POST['roomName'] ...
} //end of initial if(!empty) etc
?>
<form action="convert04.php" method="post" id="mainForm">
<fieldset class="new">
<legend>
Room Name
</legend>
<input name="roomName[]" type="text" class="new">
</fieldset>
<fieldset class="new">
<legend>
width (m)
</legend>
<input name="width[]" type="number" step=".01" class="new">
</fieldset>
<fieldset class="new">
<legend>
Length (m)
</legend>
<input name="length[]" type="number" step=".01" class="new">
</fieldset>
<input type="submit" value="Add room/update" class="submit">
</form>
</main>
<footer class="footer">
<?php include_once('includes/footer.php') ?>
</footer>
</body>
</html>