CodeIgniter Forums
Best way to create an array with query results! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best way to create an array with query results! (/showthread.php?tid=13175)



Best way to create an array with query results! - El Forum - 11-13-2008

[eluser]daweb[/eluser]
Usually.. excuse me about english :-(

I have a dynamic query with some field results (maybe cod, title) or (maybe cod, title, order) or etc... It depends on the query.

I want to build an array with the result of query

$myarray = [title] = 'MyTitle' [cod] = 'MyCod' ext...

Now, in my model code:

Code:
$query = $this->db->get('table'); // with some where clauses
if($query->num_rows() > 0 ) :
  $query_info = array(
        'list_field' => $query->list_fields(),
          'result'     => $query->result()
            );
  return $query_info;
end;

And in my library code:

Code:
$result = $this->CI->layout_Model->get_my_query();

$daweb_array = array();

# costruzione dinamica dell'array
foreach($result['list_field'] as $key => $field) :
$daweb_array['name_of_array_for_template_library'][$field] = $result['result'][0]->$field;
endforeach;

Code:
$result['result'][0]->$field

Is it the best way to do that?
Please help me CI people!

Thank you guys

PS. It works, I wanna know if it is elegant and the best way to do that


Best way to create an array with query results! - El Forum - 11-13-2008

[eluser]daweb[/eluser]
Ehi,

I think this is the better solution, I hope:

Code:
foreach($result['result'] as $res => $row) :
# costruzione dinamica dell'array
  foreach($result['list_field'] as $key => $field) :
    $daweb_array['promo'][$res][$field] = $row->$field;
  endforeach;
endforeach;