Select语句在Codeigniter中的foreach循环中不返回任何内容

时间:2017-01-25 00:47:43

标签: php codeigniter foreach

我有一个foreach循环来遍历解码的JSON对象并在每个循环上运行INSERT,但是我无法获得SELECT语句来返回同一循环中的任何内容。

这是我的模特:

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

我的结构错了吗?我使用来自url的直接控制器&gt;方法调用来运行与执行select(&#39; Type&#39;)的完全相同的代码并传入静态变量,我得到一个row_array。但它并没有在这个循环中返回任何东西。 INSERT工作正常。

1 个答案:

答案 0 :(得分:2)

尝试使用

$account = $query->row();