我正在尝试编写一个返回多维数组的函数,其中键是日期,日期还有两个项目的成本和值。如果股票市场当天关闭(IE周末或假日),我使用的结果将是零行。我想设定天数成本和价值等于前一天。显然我在这里遗漏了一些东西,但我不明白。
function getNetWorth()
{
// Query for First and Last date
$query = $this->db->query("SELECT min(date) as start from Transactions ORDER BY date ASC;");
$date = $query->row_array();
$current = $date['start'];
$networth = array();
while(strtotime($current)<= strtotime(date('Y-m-d')))
{
$cost = 0;
$value = 0;
$run = date('Y-m-d', $current);
// Query for net worth for given day
echo $current;
$sql = "SELECT Transactions.symbol, sum(shares) AS shares, sum(shares * price) AS cost, History.close as `close`, (sum(shares) * History.close) as value ".
"FROM Transactions INNER JOIN History ON (History.symbol = Transactions.symbol AND History.date ='".$run."') ".
"WHERE (action <>5) AND Transactions.date <= '".$run."' GROUP BY Transactions.symbol HAVING sum(shares) > 0";
$query = $this->db->query($sql);
if($query->num_rows() < 1)
{
$date = strtotime("-1 day", $current);
$last = date('Y-m-d', $date);
// $networth[$current][] = $networth[$last];
}
else
{
$result = $query->result_array();
$cost += $result['cost'];
$value += $result['value'];
$networth[$current]= array("cost" => $cost, "value" => $value);
}
$current = strtotime("+1 day", strtotime($current));
}
return $networth;
}
答案 0 :(得分:0)
看起来您$last
的格式与$current
的格式不同,因此$networth[$last]
不会匹配任何内容。 $last
和$run
的格式相同,因此您可能需要更改该行:
$networth[$run]= array("cost" => $cost, "value" => $value);