我正在尝试编写一个函数,该函数返回一个多维数组,其中键是日期,并且日期还有两个项目成本和价值。如果当天股市关闭(即周末或节假日),我正在使用的查询结果将为零行。我想将那一天的成本和价值设置为等于前一天。显然我在这里遗漏了一些东西,但我不明白。
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;
}