我目前正在开发一款股票市场模拟器,我试图显示每个虚拟公司的第一个生成价格。为此,我已经在PHP中生成了我的价格,并将它们添加到数组'$firstValue'中。我还为我的HTML元素生成了ID,这些值将显示在'$priceIdentifiers'中。以下是用于生成这些值的代码:
PHP:
$priceIdentifiers = array("Prices1", "Prices2", "Prices3");
$normalDistChangers = array("-1000", "1000");
$highValues = array("2500", "4850", "1780");
$lowValues = array("2200", "4300", "1400");
$firstValue = array();
for ($x = 0, $length = 3; $x < $length; $x++)
{
$tempValue = (rand($lowValues[$x], $highValues[$x]) / 100);
$tempMean = log(($tempValue) / (rand($lowValues[$x], $highValues[$x]) / 100));
$tempAnnStdDev = sqrt(365 * ($tempMean * $tempMean));
$tempNormalDist = (rand($normalDistChangers[0], $normalDistChangers[1]) / 1000);
$tempPrice = number_format(($tempValue * (1 + ($tempMean * (1/100000)) + $tempAnnStdDev * sqrt(1 / 100000) * $tempNormalDist)), 2, '.', '');
array_push($firstValue, $tempPrice);
}
以下是HTML元素的代码,价格将在其中显示。由于我目前正在与3家公司合作,因此会生成3行。
HTML和PHP:
<td style = "text-align:center;font-size:15pt" id = "<?php echo $priceIdentifiers[$a]; ?>"></td>
当$a变量的值等于0并递增直到其值等于2时。
生成数值已经成功了,我在控制台日志中看到,在显示列表内容时,不同公司的3个价格都在列表中,但是当尝试使用以下Javascript代码显示它们时,结果却显示整个数组而不是单个元素:
Javascript:
var tempPriceOne = <?php echo json_encode($firstValue); ?>;
var a = 0;
var highPrice = tempPriceOne;
var inv = setInterval(function() {
if (a < 50)
{
document.getElementById("Prices1").innerHTML = tempPriceOne;
目前,在查看网站:http://leonid.chashchin.net/stockMarket.php 时,它将整个数组显示在第一行'Price'中。我需要修改什么才能使第一个元素显示在第一行,第二个元素显示在第二行,以此类推?
$firstValue
是一个数组。您期望该单元格的值是什么? - Jeto