Tuesday, February 2, 2021

Area of Shapes (Rectangle, Triangle, and Circle) in PHP

Machine Problem in PHP

Create a PHP program that will help the USERS to compute the area of a rectangle, triangle, and circle. Use functions in creating the program. Apply CSS to have an appealing program. Expected outputs: *must have an input box for measurements *the program will ask the user if it's a rectangle, triangle, or circle (use a dropdown list) *displays the correct area *button/s that will compute the area.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City I also accepting computer repair, networking, and Arduino Project development at a very affordable price. My website is www.jakerpomperada.blogspot.com and www.jakerpomperada.com

If you like this video please click the LIKE button, SHARE, and SUBSCRIBE to my channel.

Your support on my channel is highly appreciated.

Thank you very much.







Program Listing

style.css

* { box-sizing: border-box; } body { font-family: Arial, Helvetica, sans-serif; background: #fefefe; color: #1a1a1a; font-size: 15px; } main { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 500px; box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px; border-radius: 10px; } .container { display: block; padding: 30px; background: #fff; text-align: left; border-radius: 2px; } h2 { text-align: center; } label { width: 100%; font-size: 20px; } input, select { display: inline-block; background: #f5f5f5; width: 100%; margin: 5px 0 20px 0; padding: 15px; border: none; font-size: 15px; } input:focus, select:focus { background: #eeeeee; outline: none; } button { background: #2196f3; color: #fff; padding: 15px; margin: 0 0 20px; border: none; cursor: pointer; width: 100%; opacity: 0.9; font-size: 15px; outline: none; } .btnSelectShape { background: #f44336; } button:hover { opacity: 1; } .answer { width: 100%; padding: 20px; text-align: center; font-size: 30px; font-weight: bold; background: #dadada; font-family: 'Times New Roman', serif; line-height: 1.5; } .visibility_on { display: block; } .visibility_off { display: none; } .frmAreaSelect { display: flex; flex-flow: row wrap; align-items: center; } .frmAreaSelect select { vertical-align: middle; padding: 15px; margin: 5px 0 0; width: calc(100% - 100px); } .frmAreaSelect button { width: 100px; margin: 5px 0 0; } .pieText { font-family: Serif; }


index.php

<?php /* Area of Shapes (Rectangle, Triangle, and Circle) in PHP Author : Jake Rodriguez Pomperada, MAED-IT, MIT www.jakerpomperada.com and www.jakerpomperada.blogspot.com jakerpomperada@gmail.com Bacolod City, Negros Occidental Philippines */ $area = $html = $infotext = $sel1 = $sel2 = $sel3 = ""; $num1 = isset($_POST['txtNum1']) ? floatval($_POST['txtNum1']) : ""; $num2 = isset($_POST['txtNum2']) ? floatval($_POST['txtNum2']) : ""; $visibility = "visibility_off"; $frmArea = isset($_GET['selectArea']) ? $_GET['selectArea'] : ''; // Display selected shape input fields if(isset($_GET['selectArea'])) { $visibility = "visibility_on"; switch ($frmArea) { case "Rectangle": $sel1 = "selected"; $html = '<h2>Area of a Rectangle ( l x w )</h2> <label for="txtNum1"><b>Length (l):</b></label> <input type="number" step=".0001" name="txtNum1" id="txtNum1" value="'.$num1.'" required> <label for="txtNum2"><b>Width (w):</b></label> <input type="number" step=".0001" name="txtNum2" id="txtNum2" value="'.$num2.'" required>'; if(isset($_POST['btnCalculate'])) { $area = round($num1 * $num2); $infotext = '<div class="answer">'.$num1." x ".$num2." = ".$area."</div>"; } break; case "Triangle": $sel2 = "selected"; $html = '<h2>Area of a Triangle ( <sup>bh</sup>&frasl;<sub>2</sub> )</h2> <label for="txtNum1"><b>Base (b):</b></label> <input type="number" step=".0001" name="txtNum1" id="txtNum1" value="'.$num1.'" required> <label for="txtNum2"><b>Height (h):</b></label> <input type="number" step=".0001" name="txtNum2" id="txtNum2" value="'.$num2.'" required>'; if(isset($_POST['btnCalculate'])) { $area = round(($num1 * $num2)/2); $infotext = '<div class="answer"><sup>('.$num1." x ".$num2.")</sup>&frasl;<sub>2</sub> = ".$area."</div>"; } break; case "Circle": $sel3 = "selected"; $html = '<h2>Area of a Circle ( <span class="pieText">π</span>r<sup>2</sup> )</h2> <label for="txtNum1"><b>Radius (r):</b></label> <input type="number" step=".0001" name="txtNum1" id="txtNum1" value="'.$num1.'" required>'; if(isset($_POST['btnCalculate'])) { $area = round((pi() * pow($num1, 2)), 4); $infotext = '<div class="answer"><span class="pieText">π</span>('.$num1.')<sup>2</sup> = '.$area.'</div>'; } break; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Area of Shapes (Rectangle, Triangle, and Circle) in PHP</title> <link rel="stylesheet" href="style.css"> </head> <body> <h3 align="center"> Area of Shapes (Rectangle, Triangle, and Circle) in PHP </h3> <main> <div class="container"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" class="frmAreaSelect"> <label for="selectArea"><b>Select Shape:</b></label> <select name="selectArea" id="selectArea" value="<?php echo $oper; ?>" required> <option value="Rectangle" <?php echo $sel1; ?>>Rectangle</option> <option value="Triangle" <?php echo $sel2; ?>>Triangle</option> <option value="Circle" <?php echo $sel3; ?>>Circle</option> </select> <button type="submit" class="btnSelectShape">Select</button> </form> </div> <div class="container <?php echo $visibility; ?>"> <form action="" method="post" class="frmAreaCalculate"> <?php echo $html; ?> <button type="submit" name="btnCalculate" class="btnCalculate">Calculate Area</button> <?php echo $infotext; ?> </form> </div> </main> </body> </html>


No comments:

Post a Comment