Calculate Average Stock Price and Quantity [PHP + HTML+ CSS Code]

Calculate Average Stock Price

The code is designed to receive input from the user in the form of stock quantity and price values. The user can input up to 10 different sets of stock values, and the code will calculate the average price of all the stocks based on the total value of the stocks and the total quantity of the stocks.

Let’s break down the code and see how it works:

PHP and HTML Code to Get Data

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculate Average Stock Price</title>
<style>
/* CSS styles go here */
</style>
</head>
<body>
<h1>Calculate Average Stock Price</h1>
<form method="post">
<?php
for($i = 1; $i <= 10; $i++) {
?>
<!-- Input fields for stock quantity and price go here -->
<?php
}
?>
<button type="submit">Calculate</button>
</form>
<?php
// PHP code to calculate average stock price goes here
?>
</body>
</html>

The code starts with an HTML document that includes a title, some CSS styles, and a header with the title of the page. Inside the body of the document, there is an HTML form with a method of “post”, which means that the form data will be sent to the server as an HTTP POST request. The form includes a for loop that generates up to 10 sets of input fields for the stock quantity and price.

<label>
Enter the quantity for stock <?php echo $i; ?>:
<input type="number" name="quantity[]" min="1">
</label>
<label>
Enter the price per unit for stock <?php echo $i; ?>:
<input type="number" name="price[]" min="0.01" step="0.01">
</label>

The for loop generates two label/input pairs for each iteration. The first label/input pair is for the stock quantity, and the second label/input pair is for the stock price. The label text includes the value of the $i variable, which corresponds to the current iteration of the loop. The name attribute of each input field ends with [], which allows multiple values to be submitted for that field.

Calculate Average Stock Price

The input fields have some attributes to limit and validate the user’s input. The min attribute on the quantity input field ensures that the user inputs a positive integer value, while the min and step attributes on the price input field ensure that the user inputs a positive decimal value with up to two decimal places.

PHP Calculate the Average Stock Price and Quantity

<?php
if(isset($_POST['quantity']) && isset($_POST['price'])) {
$quantity_array = $_POST['quantity'];
$price_array = $_POST['price'];
$total_value = 0;
$total_quantity = 0;
for($i = 0; $i < count($quantity_array); $i++) {
$quantity = intval($quantity_array[$i]);
$price = floatval($price_array[$i]);
$total_value += $quantity * $price;
$total_quantity += $quantity;
}
$average_price = $total_value / $total_quantity;
echo "<div class='result-quantity'>The average price of the stock is " . number_format($total_quantity, 2) . "</div>";
echo "<div class='result'>The average price of the stock is " . number_format($average_price, 2) . "</div>";
}
?>

When the user submits the form it will Calculate the Average Stock Price and Quantity

CSS Code

<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #f7f7f7;
}
.insertcart-stock-price {

}
label {
display: block;
margin-bottom: 10px;
}
input[type="number"] {
padding: 5px;
font-size: 16px;
}
button[type="submit"] {
padding: 5px 10px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #3e8e41;
}
.result {
font-size: 20px;
font-weight: bold;
margin-top: 10px;
}
</style>

You can Download the Full Code From Github

Sandy

Sandy

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *