PHP 从另一列数据自动递增(浮动)列
PHP Auto Increment a (float) column from another column data
我的程序是这样开始的:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
其中:
B = Buy
S = Sell
and
P = Profit.
我是这样计算值的:
bv = 18.60; //fixed value
sv = 19.45; //fixed value
START = 4000; //fixed value
B = (START/bv);
S = (B*sv);
P = (S-START);
我如何设法将 "SELL" 值作为循环存储在 "START" 中并接收 table,如下所示:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
4182.72| 224.87 | 4373.72 | 191.00 |
??
我试过:
$start= 4000;
//$inic = $cashinic;
$bv= 18.6; //buy value
$sv= 19.45;//sell value
$buy= ($start/$bv);
$sell = ($comp*$sv);
$profit = ($sell-$start);
for($i = 1; $i<=100; $i++)
{
?>
<tr>
<td><div align="center"><?php echo $start; ?> </div></td>
<td><?php echo $buy; ?></td>
<td><?php echo $sell; ?></td>
<td><div align="center"><?php echo $profit; ?> </div></td>
</tr>
<?php
}
?>
</table>
但它只给我这个结果:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
4000 | 215.05 | 4182.72 | 182.72 |
4000 | 215.05 | 4182.72 | 182.72 |
.... | ...... | ....... | ...... |
等等...
非常感谢您的帮助。
我是否需要另一列作为自动递增列?
像这样:
Col_index |START | B | S | P |
1 | 4000 | 215.05 | 4182.72 | 182.72 |
2 | ... | ... | ... | ... |
P.D。我没有使用数据库,有必要吗? (主要是因为我觉得我不需要数据库)
您需要在循环结束时更新 $start
,以便下一次迭代使用新值:
$start = 4000;
$bv = 18.6;
$sv = 19.45;
for($i = 1; $i <= 100; $i += 1) {
$buy = $start / $bv;
$sell = $buy * $sv; // I replaced $comp with $buy is that correct?
$profit = $sell - $start;
echo $start . "\t" . $buy . "\t" . $sell . "\t" . $profit . "\n";
// update start
$start = $sell;
}
输出:
4000 215.05376344086 4182.7956989247 182.79569892473
4182.7956989247 224.8814891895 4373.9449647358 191.14926581108
4373.9449647358 235.15833143741 4573.8295464576 199.8845817218
4573.8295464576 245.90481432568 4782.8486386344 209.01909217683
...
如果您不喜欢递增 $start
(您可能会出现舍入错误),您还可以通过使用一点将 $start
设置为 row-$i
的值聪明的数学:
$start = 4000;
$bv = 18.6;
$sv = 19.45;
for($i = 0; $i < 100; $i += 1) { // loop needs to start at 0
$s = $start * pow($sv / $bv, $i);
$buy = $s / $bv;
$sell = $buy * $sv;
$profit = $sell - $start;
echo $s . "\t" . $buy . "\t" . $sell . "\t" . $profit . "\n";
}
我的程序是这样开始的:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
其中:
B = Buy
S = Sell
and
P = Profit.
我是这样计算值的:
bv = 18.60; //fixed value
sv = 19.45; //fixed value
START = 4000; //fixed value
B = (START/bv);
S = (B*sv);
P = (S-START);
我如何设法将 "SELL" 值作为循环存储在 "START" 中并接收 table,如下所示:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
4182.72| 224.87 | 4373.72 | 191.00 |
??
我试过:
$start= 4000;
//$inic = $cashinic;
$bv= 18.6; //buy value
$sv= 19.45;//sell value
$buy= ($start/$bv);
$sell = ($comp*$sv);
$profit = ($sell-$start);
for($i = 1; $i<=100; $i++)
{
?>
<tr>
<td><div align="center"><?php echo $start; ?> </div></td>
<td><?php echo $buy; ?></td>
<td><?php echo $sell; ?></td>
<td><div align="center"><?php echo $profit; ?> </div></td>
</tr>
<?php
}
?>
</table>
但它只给我这个结果:
START | B | S | P |
4000 | 215.05 | 4182.72 | 182.72 |
4000 | 215.05 | 4182.72 | 182.72 |
4000 | 215.05 | 4182.72 | 182.72 |
.... | ...... | ....... | ...... |
等等... 非常感谢您的帮助。
我是否需要另一列作为自动递增列?
像这样:
Col_index |START | B | S | P |
1 | 4000 | 215.05 | 4182.72 | 182.72 |
2 | ... | ... | ... | ... |
P.D。我没有使用数据库,有必要吗? (主要是因为我觉得我不需要数据库)
您需要在循环结束时更新 $start
,以便下一次迭代使用新值:
$start = 4000;
$bv = 18.6;
$sv = 19.45;
for($i = 1; $i <= 100; $i += 1) {
$buy = $start / $bv;
$sell = $buy * $sv; // I replaced $comp with $buy is that correct?
$profit = $sell - $start;
echo $start . "\t" . $buy . "\t" . $sell . "\t" . $profit . "\n";
// update start
$start = $sell;
}
输出:
4000 215.05376344086 4182.7956989247 182.79569892473
4182.7956989247 224.8814891895 4373.9449647358 191.14926581108
4373.9449647358 235.15833143741 4573.8295464576 199.8845817218
4573.8295464576 245.90481432568 4782.8486386344 209.01909217683
...
如果您不喜欢递增 $start
(您可能会出现舍入错误),您还可以通过使用一点将 $start
设置为 row-$i
的值聪明的数学:
$start = 4000;
$bv = 18.6;
$sv = 19.45;
for($i = 0; $i < 100; $i += 1) { // loop needs to start at 0
$s = $start * pow($sv / $bv, $i);
$buy = $s / $bv;
$sell = $buy * $sv;
$profit = $sell - $start;
echo $s . "\t" . $buy . "\t" . $sell . "\t" . $profit . "\n";
}