Jason Ayer
Jason Ayer

Reputation: 597

Select statement returning nothing in foreach loop in Codeigniter

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.

Upvotes: 0

Views: 192

Answers (1)

Name
Name

Reputation: 408

Try to use

$account = $query->row();

Upvotes: 2

Related Questions