0

I have a foreach loop to loop through a decoded JSON object and run an INSERT on each loop, however I cannot get a SELECT statement to return anything within the same loop.

Here is my Model:

foreach (json_decode($detail, TRUE) as $key => $value)
    {
        $this->db->select('Type');
        $this->db->where(array('AssociationId' => $id, 'BeginDate' => $year, 'Account' => $value['account']));
        $query = $this->db->get('GLAccounts');

        $account = $query->row_array(); // <- nothing being returned here. Only one row should be returned

        if($account['Type'] == 'Asset' || $account['Type'] == 'Expense') // <- so this is being ignored
        {
            $value['debit'] = 1 * $value['debit'];
            $value['credit'] = -1 * $value['credit'];
        }

        else if($account['Type'] == 'Liablity' || $account['Type'] == 'Capital' || $account['Type'] == 'Income') // <- as is this
        {
            $value['debit'] = -1 * $value['debit'];
            $value['credit'] = 1 * $value['credit'];
        }

        $values = array(
            'id' => NULL,
            'JournalId' => $id,
            'Date' => $this->input->post('date'),
            'Account' => $value['account'],
            'Description' => $value['description'],
            'Debit' => $value['debit'],
            'Credit' => $value['credit']
        );

        $this->db->insert('GLJournalDetails', $values);

Is my structure wrong? I'f I run that EXACT same code that does the select('Type') using a direct controller->method call from a url and pass in static variables, I get a row_array back. But it's not returning anything inside this loop. The INSERT works just fine.

3
  • Did you try this $account = $query->result_array(); insted of row_array(). Commented Jan 25, 2017 at 3:29
  • I'm an idiot, I actually not even 30 seconds after posting this found my error. I was reassigning my $id variable when it shouldn't have been and thus was passing a different value to look for on SELECT and rightfully so returning nothing... Commented Jan 25, 2017 at 3:41
  • Solving our error by are a self is a good experience. Keep Coding. Commented Jan 25, 2017 at 3:44

1 Answer 1

2

Try to use

$account = $query->row();
Sign up to request clarification or add additional context in comments.

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.