每天细菌计数的php输出循环
在这里,我计算了孵化10天后存在的细菌总数。用户只需输入初始细菌的数量,并返回结果。我下一步要做的事情让我有些困惑。我现在的目标是在一个循环中完成这项工作,该循环还将在web上的表中生成输出 页面如下所示。x表示当天的细菌总数。有人知道如何在php中执行吗每天细菌计数的php输出循环,php,loops,for-loop,Php,Loops,For Loop,在这里,我计算了孵化10天后存在的细菌总数。用户只需输入初始细菌的数量,并返回结果。我下一步要做的事情让我有些困惑。我现在的目标是在一个循环中完成这项工作,该循环还将在web上的表中生成输出 页面如下所示。x表示当天的细菌总数。有人知道如何在php中执行吗 Initial Bacteria present: Day: Bacteria: 1 x . x . x . x 10 x 这是我的php/html用于计算 <?php error_r
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 />";
}
}
?>
这是我在$x
的值中输入30
时的输出:
你试过什么?如果$total=$x*pow(2,$days)
为您提供了$days
days的结果。计数如何从dayn
更改为n+1
?我们在正确的轨道上。我现在得到的是未定义的索引:第6行的/home/user/public_html/index2.php中的initialBacteria当我提交时,通知会消失,但我仍然想知道如何删除它。哦,我忘了添加if(isset($\u POST['submit']){}
我会编辑我的答案