在这里,我计算了培养10天后的细菌总数。用户只需输入初始细菌的数量并返回结果。我下一步要做的事让我有些困惑。我现在的目标是在一个循环中完成这项工作,这个循环也会在web上的一个表中生成输出
页面如下所示。X代表当天细菌总数。有人知道如何在php中执行吗?
Initial Bacteria present:
Day: Bacteria:
1 x
. x
. x
. x
10 x
这是我用来计算的php/html。
<?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>
最佳答案
您可以循环浏览天数,然后将结果插入到数组中。
将数据插入数组后,可以使用foreach
循环来回送数组中的每个结果。
<?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 />";
}
}
?>
这是我在
30
值中输入$x
时的输出:关于php - 每天来自细菌计数的php输出循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31237327/