我在两个不同的查询中从表中提取数据,我正在尝试比较两个结果中的日期字段。当日期相等时,我想将每个查询的两个结束余额加在一起,并将结果放入一个新数组中。我遇到的问题是on line 59
,我得到的错误是
Notice: Undefined offset
。
这就是我所拥有的:
include "../sqlConnect.php";
mysqli_set_charset($dbScrap, 'utf8');
$dataArray[] = array();
$dateArray[] = array();
$balanceArray[] = array();
//Smaller
$query = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account= '5010-15-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap, $query) or die("SQL Error 1: " .
mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$dateArray[] = array(
'Date' => $Date15
);
$balanceArray[] = array(
'EndingBalance' => $EndingBalance15
);
}
\\Bigger
$query1 = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account ='5010-08-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap,$query1) or die("SQL Error 1: " . mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
for($i = 0; $i < $dateArray; $i++){
if($Date08 == $dateArray[$i]) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$i];
$Date = $Date08;
}else{
$message = "Date Not Equal";
$Date = $Date08;
$EndingBalance = $EndingBalance08;
}
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date,
'Message' => $message
);
}
echo "<pre>";
print_r($dataArray);
//echo json_encode($dataArray);
echo "</pre>";
$result->close();
/* close connection */
$dbScrap->close();
感谢您的帮助。
答案 0 :(得分:2)
问题在于循环,纠正:
for ($i = 0; $i < count($dateArray); $i++){
答案 1 :(得分:1)
在您从第一个查询中获取结果的循环中,使用每行中的日期作为$balanceArray
中的键,如下所示:
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$balanceArray[$Date15] = $EndingBalance15;
}
然后在从第二个查询中获取结果的循环中,您可以使用isset
检查第一个查询的结果中是否存在该日期,如下所示。
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
if (isset($balanceArray[$Date08])) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$Date08];
} else {
$message = "Date Not Equal";
$EndingBalance = $EndingBalance08;
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date08,
'Message' => $message
);
}