title: Codeigniter $query->result() loop to associative array
body: <p>I'm having trouble getting this loop to return all of the rows that I know mysql is generating in this query.  The objects in my javascript console are returning as I would like them to; however, I'm only getting the last row from each unique <code>$row-&gt;student_id</code>.  I can tell that each unique <code>row-&gt;student_id</code> is being overwritten by the previous, but I'm not sure how else to do this logic.  </p>
  
  <p>This is part of a Codeigniter model.  My controller is performing a json_encode function on this function, then I'm pulling this data into my view with jQuery <code>$.ajax</code> wired to a handlebars.js template.  I'm aware that I could use ember for data bindings, but that is beyond my expertise at this time.</p>
  
  <pre><code>$query = $this-&gt;db-&gt;get();
      if ($query-&gt;num_rows() &gt; 0) {
  
          $prev = null;
          foreach ($query-&gt;result() as $row) {
  
              if ($prev != $row-&gt;student_id) {
                  $data[$row-&gt;student_id] = array(
                      'first' =&gt; $row-&gt;last_name,
                      'last' =&gt; $row-&gt;first_name);
                  $data[$row-&gt;student_id]['dorc'] = array($row-&gt;debit_or_credit);
                  $prev = $row-&gt;student_id;
              } else {
                  $data[$row-&gt;student_id]['dorc'][] += $row-&gt;debit_or_credit;
  
              }
          }
  
          return $data;
      } else {
          return array();
      }`
  </code></pre>