0

I'm pulling data from a table in two different queries, and I am trying to compare the date field from both results. When the date is equal I want to add the two ending balances from each query together and put the results into a new array. The problem I am having is on line 59, The error I get is Notice: Undefined offset.

This is what I have:

 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();

Thank you for your help.

11
  • What is your line 59? Commented May 30, 2017 at 20:56
  • You're attempting to loop through $dateArray but at the end of the loop you append another element. This means the loop will never exit. Commented May 30, 2017 at 20:57
  • @swapgs if($Date08 == $dateArray[$i]) is line 59 Commented May 30, 2017 at 20:59
  • $Date08 is a string, $dateArray[$i] is an array. They will never be equal. Commented May 30, 2017 at 21:00
  • It looks like you shouldn't be appending arrays in your first while loop. Just use $dateArray[] = $Date15; and $balanceArray[] = $EndingBalance15;. Commented May 30, 2017 at 21:05

2 Answers 2

2

The problem is in for loop, correction:

for ($i = 0; $i < count($dateArray); $i++){
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! That worked, but the ending balances are not adding together
That just solves one specific error. There are other problems.
if($Date08 == $dateArray[$i]) should be if($Date08 == $dateArray[$i]['Date'])
@T.AKROUT I got the following error 'Illegal string offset 'Date' '
You have the error because you removed $dateArray[] = array( 'Date' => $Date15 ); and you replaced it by $dateArray[] = $Date15, now you don't have to change any thing
1

In the loop where you fetch results from your first query, use the date from each row as the key in $balanceArray, like this:

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    $EndingBalance15 = $row['Month_Total'];
    $Date15 = $row['Date'];

    $balanceArray[$Date15] = $EndingBalance15;
}

Then in the loop where you fetch results from your second query, you can use isset to check if that date exists in the results from the first query, like this.

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
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.