Output cycle in php of bacteria per day
Here I have calculated the total amount of bacteria present after 10 days of incubation. The user simply enters the amount of the original bacteria and the result is returned. What I am trying to do next confuses me somewhat. Now my goal is to do this in a loop which also generates the output in a table on the internet as shown below. x represents the total bacteria count for that day. Does anyone know how to accomplish this in php?
Initial Bacteria present:
Day: Bacteria:
1 x
. x
. x
. x
10 x
Here is my php / html for the calculation.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
if(isset($_POST['submit'])) {
$x = (int)$_POST['initialBacteria'];
$days = 10;
$total = $x * pow(2, $days);
echo "There are: ", $total, " bacterium present in the culture after 10 days of incubation."; }
//loop goes here
?>
<!DOCTYPE html>
<html>
<head>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div align="center" id="main">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
Initial Bacterium:<input class="text" name="initialBacteria" type="text" size="15"><br><br>
<input class="text" type="submit" name="submit" value="Calculate" />
</form>
<hr>
</div>
</body>
</html>
source to share
You can loop through the days and then insert the results into an array.
After inserting data into the array, you can use a loop foreach
to echo the result of each result in the array.
<?php
if(isset($_POST['submit'])) {
$x = (int)$_POST['initialBacteria'];
$days = 10;
$total = array();
for ($i=1; $i <= $days; $i++) {
$total[$i] = $x * pow(2, $i);
}
foreach ($total as $totalKey => $totalValue) {
echo "There are: ", $totalValue, " bacterium present in the culture after ".$totalKey." days of incubation.<br />";
}
}
?>
Here's my output when I typed 30
in the value $x
:
source to share