Your cart is currently empty!
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.
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
Comments
Grabber Pro
Original price was: $59.$39Current price is: $39.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19.Android App for Your Website
Original price was: $49.$35Current price is: $35.Abnomize Pro
Original price was: $30.$24Current price is: $24.Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Handle Sudden Traffic Spike in Website – Do Node Balancer Really Help
- How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
- How to Upload Images and PDFs in Android Apps Using Retrofit
- [Fix] File Permission Issue in Apache Website WordPress Not Writable to 775
- Best PHP ini Settings for WordPress & WooCommerce: Official Recommendations
Leave a Reply